Discuss Scratch

ChildEmperorOPM
New to Scratch
2 posts

Help with animation

I need help to stop my sprite's Idle Animation when they are using a move. I want it to go back once they are done with their move, here is the code:

define (Idle Animations)
forever
repeat until <key [ any] pressed?>
switch costume to [ Idle]
wait (0.1) secs
next costume
wait (0.1) secs
next costume
wait (0.1) secs
next costume
wait (0.1) secs
next costume
wait (0.1) secs
next costume
wait (0.1) secs
next costume
wait (0.1) secs
next costume
end
wait until <<not <key [ any] pressed?>
wait (5) secs
end

Thats the code for the Idle animation. Thank you
toasty_mc_toastface
Scratcher
97 posts

Help with animation

You could try this, i included a script for running as well:

when green flag clicked
set rotation style [left/right]
forever
if <key [left arrow] pressed?> then
point in direction (-90)
run animation [left arrow]
end
if <key [right arrow] pressed?> then
point in direction (90)
run animation [right arrow]
end

when green flag clicked
forever
if <(other animation is playing) = [False]> then
idle animation
end
end


define Idle animation
set [counter] to [0]
switch costume to [idle]
wait (0.1) secs
repeat until <<(other animation is playing) = [True]> or <(counter) = [7]>>
next costume
wait (0.1) secs
change [counter] by (1)
end

define run animation (arrow key being pressed)
set [other animation is playing] to [True]
set [counter] to [0]
switch costume to [run]
wait (0.1) secs
repeat until <<<key (arrow key being pressed) pressed?>= [False]> or <(counter) = [however many run sprites there are -1]>>
next costume
wait (0.1) secs
change [counter] by (1)
end
set [other animation is playing] to [False]

Hope this helps, tell me if you need anything else

Last edited by toasty_mc_toastface (April 2, 2024 09:05:40)

deck26
Scratcher
1000+ posts

Help with animation

My preferred solution to things like this is to only have one script handling costume changes. That script then uses min and max variables to control which costumes it uses. Then when the character's status changes you just need to change the min and max to correspond to the current state.

For example running might set min to 1 and max to 5, walking sets min to 6 and max to 8, idle sets min and max to 9 and so on.
toasty_mc_toastface
Scratcher
97 posts

Help with animation

Good idea. Ill do an example in case they want t do it that way
deck26
Scratcher
1000+ posts

Help with animation

toasty_mc_toastface wrote:

Good idea. Ill do an example in case they want t do it that way
https://scratch.mit.edu/projects/173573727 does this albeit with pointless costumes.
toasty_mc_toastface
Scratcher
97 posts

Help with animation

when green flag clicked
forever
if <(costume #) = (max costume)> then
switch costume to [min costume]
else
next costume
end
wait (0.1) secs
end

when green flag clicked
set rotation style [left/right]
forever
if <key [left arrow] pressed?> then
point in direction (-90)
set (min costume) to [4]
set (max costume) to [6]
else
if <key [right arrow] pressed?> then
point in direction (90)
set (min costume) to [4]
set (max costume) to [6]
else
set (min costume) to [1]
set (max costume) to [3]
end
end

that code is much simpler, sorry for overcomplicating (this code would be for if the idle animation was costumes 1 to 3 and running 4 to 6)
deck26
Scratcher
1000+ posts

Help with animation

toasty_mc_toastface wrote:

when green flag clicked
forever
if <(costume #) = (max costume)> then
switch costume to [min costume]
else
next costume
end
wait (0.1) secs
end

when green flag clicked
set rotation style [left/right]
forever
if <key [left arrow] pressed?> then
point in direction (-90)
set (min costume) to [4]
set (max costume) to [6]
else
if <key [right arrow] pressed?> then
point in direction (90)
set (min costume) to [4]
set (max costume) to [6]
else
set (min costume) to [1]
set (max costume) to [3]
end
end

that code is much simpler, sorry for overcomplicating (this code would be for if the idle animation was costumes 1 to 3 and running 4 to 6)
You need to set the costume to the first costume for the new range when the range changes. Imagine you were in the group 1 to 5 and switching to 10 to 15 - your code would have to go through all the intervening costumes. Similarly if going the other way you'd have to go to the end of the entire costume list and loop round to the start.
ChildEmperorOPM
New to Scratch
2 posts

Help with animation

Thank you. I will try it and see if it will work.

Powered by DjangoBB