Discuss Scratch

dew-nose
Scratcher
4 posts

Walking Animation

Hi!
I haven't been on Scratch in a while and I have started working on the first game on this new account (I deleted my old one). I'm trying to do a walking animation, which I have got to work, however when you stop pressing the arrow keys the walking animation continues until it gets to the end, whereas I want it to stop immediately and go back to the standing position.
The script I am using is a separate basic movement script, and (for all directions):

when green flag clicked
forever

if <key [right arrow] pressed?> then
repeat until <not <key [right arrow] pressed?>>
switch costume to [standing still]
wait (0.2) secs
switch costume to [front leg forward]
wait (0.2) secs
switch costume to [standing still]
wait (0.2) secs
switch costume to [back leg forward]

end
switch costume to [standing still]

end

end

I would appreciate any help on a better way to do this script, in a way that makes the animation stop as soon as you stop pressing the key. Thank you!

dew-nose
P444
Scratcher
500+ posts

Walking Animation

Maybe this? One script has to executed so it will finish immediately, whereas your loop will finish scripts inside and stop.
forever

if <key [right v] pressed?> then

set [walking v] to [1]
next costume//assuming that all the costumes are in order
wait (0.1) secs//any number you want
else
If <(key pressed) = [0]>
set [walking v] to [0]
End
end
if <(walking) = [0]> then

switch costume to [standing still v]


end
if <key [any v] pressed?> then//replace any keys with movement keys with or blocks

set [key pressed v] to [1]
Else
Set [key pressed v] to [0]
end


end

Edit: The walking true or false script is to put 1 loop to stop the character. Replace the right arrorw key with any key you want and set walking to 1 when pressed and when not, 0

Last edited by P444 (Feb. 16, 2016 17:10:04)

dew-nose
Scratcher
4 posts

Walking Animation

P444 wrote:

Maybe this? One script has to executed so it will finish immediately, whereas your loop will finish scripts inside and stop.
forever

if <key [right v] pressed?> then

next costume//assuming that all the costumes are in order
wait (0.1) secs//any number you want
else
switch costume to [standing still v]
end

end


Thank you for replying so quickly, but I don't think this would work for me as the sprite has other costumes. I might be able to figure something out using that though…

edit: Oh, I just saw your edit after I poster this, thank you

Last edited by dew-nose (Feb. 16, 2016 16:47:54)

P444
Scratcher
500+ posts

Walking Animation

If you have other costumes, arrange all walking costumes in order and use this:
forever

if <<key [right arrow v] pressed?> and <(key pressed) = [1]>> then

set [walking v] to [1]
change [costume v] by [1]
if <(costume) > [6]> then//replace 6 with last walking costume number

set [costume v] to [1]
end
switch costume to (costume)
wait (0.1) secs//any number you want
else
If <(key pressed) = [0]>
set [walking v] to [0]
End
end
if <(walking) = [0]> then

set [costume v] to [1]//replace 1 with standing still costume number
switch costume to [standing still v]


end
if <<key [right arrow v] pressed?> and <not <key [left arrow v] pressed?>>> then//assuming you have 2 directional movement

set [key pressed v] to [1]
Else
if <key [left arrow v] pressed?>
Set [key pressed v] to [2]
else
Set [key pressed v] to [0]
end


end

Last edited by P444 (Feb. 16, 2016 17:28:34)

P444
Scratcher
500+ posts

Walking Animation

Building blocks on forum was too hard so here is an example!
https://scratch.mit.edu/projects/98080939/

Powered by DjangoBB