Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » how do you make a sprite be animated while moving?
- cwhq_waylonc
-
Scratcher
5 posts
how do you make a sprite be animated while moving?
how do you make a sprite be animated while moving?
- Jman1111
-
Scratcher
54 posts
how do you make a sprite be animated while moving?
There are a number of ways to achieve this. I recommend making costumes named by the type of animation it's doing and then the keyframe of the animation after it.
If you have a sprite with a walk animation with 3 frames.
name the costumes: “walk0”, “walk1”, “walk2”
For example, this script will handle movement.
This script will handle which frame of animation to use.
Frame delay can be whatever you want for the delay between frames.
The “3” in the mod operations is the total number of frames in the animation.
If you have multiple animations for a sprite you may want to make an animation state variable, and plug that in for “walk” in the join block.
Hope this helped a bit. There is a lot of cool different things you can do with this and expand upon it.
You can also add a variable for pausing the animation if you don't want the frame variable to increase constantly.
If you have a sprite with a walk animation with 3 frames.
name the costumes: “walk0”, “walk1”, “walk2”
For example, this script will handle movement.
when green flag clicked
forever
if <key [right v] pressed?> then
change x by (5)
end
if <key [left v] pressed?> then
change x by (-5)
end
end
This script will handle which frame of animation to use.
when green flag clicked
set [frame v] to [0]
forever
set [frame v] to (((frame) + (1)) mod (3))
switch costume to (join [walk] (frame))
wait (frameDelay) secs
end
Frame delay can be whatever you want for the delay between frames.
The “3” in the mod operations is the total number of frames in the animation.
If you have multiple animations for a sprite you may want to make an animation state variable, and plug that in for “walk” in the join block.
switch costume to (join (animationState) (frame))
Hope this helped a bit. There is a lot of cool different things you can do with this and expand upon it.
You can also add a variable for pausing the animation if you don't want the frame variable to increase constantly.
Last edited by Jman1111 (Aug. 13, 2020 20:15:31)
- cwhq_waylonc
-
Scratcher
5 posts
how do you make a sprite be animated while moving?
wait until <>I recieve message 1
- Pixlelated_Dev122
-
Scratcher
7 posts
how do you make a sprite be animated while moving?
some of theses doesn't help 

- Lemoneer
-
Scratcher
63 posts
how do you make a sprite be animated while moving?
An example for moving left and right on a screen could work something like this using a variable:
The “mod” operator block “loops” the variable back, so instead of 1, 2, 3, 4, 5, it's 1, 2, 0, 1, 2, 0. This means you'll need different costumes named left0, left1, left2, right0, etc. The second part of the mod block is determined by how many costumes the sprite has. If the sprite has 2 left costumes and 2 right costumes, you'll need to make the block:
I hope this helps!
when green flag clicked
forever
if <key [right arrow v] pressed?> then
switch costume to (join [right] ((variable) mod (3)))
change x by (10)
change [variable v] by (1)
end
if <key [left v] pressed?> then
switch costume to (join [left] ((variable) mod (3)))
change x by (-10)
change [variable v] by (1)
end
end
The “mod” operator block “loops” the variable back, so instead of 1, 2, 3, 4, 5, it's 1, 2, 0, 1, 2, 0. This means you'll need different costumes named left0, left1, left2, right0, etc. The second part of the mod block is determined by how many costumes the sprite has. If the sprite has 2 left costumes and 2 right costumes, you'll need to make the block:
((variable) mod (2))
I hope this helps!
Last edited by Lemoneer (Aug. 18, 2023 18:45:37)
- Pixilized
-
Scratcher
100+ posts
how do you make a sprite be animated while moving?
Hi! It’s great that you’re trying to help, but this topic looks pretty old. Let’s try to pay attention to the date the topic was made to avoid necroposting. Thanks!
- Discussion Forums
- » Help with Scripts
-
» how do you make a sprite be animated while moving?