Smooth out sprite motion in scratch
Asked Answered
W

7

6

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?

Welladvised answered 1/9, 2011 at 14:54 Comment(0)
O
4

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.

Obstruct answered 30/5, 2013 at 10:51 Comment(0)
D
3

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.

Dogmatist answered 6/1, 2016 at 10:36 Comment(2)
WARNING: Error in code! Where's the resetting of xs? The value of xs will very quickly go from 1 to 0.8 to 0.64 to 0.512 to 0.4096... and that's assuming it starts at 1! Because there's no set [xs v] to (number), the value will never reset!Kaffiyeh
The code works. Any value of xs becomes exponentially smaller when the right arrow key is not pressed. So xs approaches zero when no key pressed and a value of 4 (calculated by 0.8/(1-0.8)) when the right arrow key is kept pressed.Rese
C
2

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. :)

Canaanite answered 10/11, 2011 at 18:31 Comment(0)
W
2

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.

Weismann answered 29/11, 2011 at 14:32 Comment(0)
K
1

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:

repeating an action until a key no longer pressed

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.

Kantian answered 27/3, 2020 at 4:7 Comment(0)
N
0

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.

Nappy answered 27/6, 2016 at 18:7 Comment(0)
A
0

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])
Affaire answered 29/11, 2020 at 8:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.