Smooth out sprite motion in scratch
How can I smooth out animations in Scratch? Mainly, I want it such that when you press and hold the right arrow, it goes right without any noticeable jittering. 开发者_如何学编程Plus, scratch makes you wait a second to repeat when you hold an arrow. How can I smooth this stuff out?
I know it's an old question. But in case somebody is looking for a solution, check out this scratch project: http://scratch.mit.edu/projects/276298/
Just add forever loop and inside this loop check if arrow key is pressed. If it is - move. This way you won't be dependent on keyboard repeat rate.
With Scratch, you can get very smooth motion using Glide
with long distances or intervals. However, the disadvantage of this method is that the Glide operation must finish before the sprite can do any sensing, such as edge or collision detection. This is often undesirable.
The small delay that you are talking about when you press a key, is actually directly tied to the repeat rate of your keyboard. When you press a key on your keyboard, that key event is sent, but then there is a small delay before the repeat kicks in. If you can find a way to change your system keyboard repeat rate, this would carry over into Scratch.
There is a limit to how much optimizing you can do in Scratch. It is, after all, a very basic(but very fun), entry-level programming environment. :)
You could do until "arrow not pressed"
when arrow pressed repeat move (or glide) until not arrow pressed
then it will not check key pressing at set intervals, making the move smoother.
You can do this:
When flag pressed
forever
if (right arrow pressed?) then
change xs by 1
xs = xs * 0.8
change x by (xs)
That checks everytime if the right arrow is pressed, and if yes, it changes the variable xs
by one. Later, it multiplies xs by 0.8, so the value sightly decays.
Then it changes the sprite's x by the var xs
.
This is an oldie but a goodie, and as others have suggested you need to keep checking that the key is still pressed and repeat whatever action you need until it's not pressed any more. Like so:
This is because when you hold down a key the computer will wait a short pause before accepting that you really do want the the key press to be repeated.
This is a combination of concepts called debouncing
(or throttling
) an input and is useful on all kinds of electronics and computing). If it didn't do that allll ooofff yoourr ttyping wwoould look likke thhhis.
One way you can do that is use variables and custom blocks to make arguments to smooth out motion. Add a variable called speed x and use a forever change X by speed X. I don't have any snapshots or examples, but you can use scripts in the custom block to smoothly glide it.
You could do this for smooth x move:
forever
change x by (([amount] - (x position)) / [divide])
Which 'amount' is how much that you want to move and 'divide' is how smooth is that. (Do the same with smoothing y)
Or you can also smooth x and y move:
forever
go to x: (([amount] - (x position)) / [divide]) y: (([amount] - (x position)) / [divide])
Also, you can make the sprite glide smoothly to the mouse:
forever
go to x: (((mouse x) - (x position)) / [divide]) y: (((mouse y) - (x position)) / [divide])
精彩评论