From Test-Scratch-Wiki
Animating a sprite is the process of a sprite repeatedly changes costumes, creating an animation. Animation scripts vary, depending on the amount of costumes and speed.
Creating an Animating Script
The simplest way to animate a sprite is to repeatedly use the Switch Costume to () block and Wait () Secs:
switch costume to [Slash1 v] wait (0.05) secs switch costume to [Slash2 v] wait (0.05) secs switch costume to [Slash3 v] wait (0.05) secs switch costume to [Slash4 v] wait (0.05) secs switch costume to [Slash5 v] wait (0.05) secs switch costume to [Slash6 v]
As you can see, it is lengthy and can easily be done in a more efficient way:
switch costume to [Slash1 v] repeat (5) wait (0.05) secs next costume end
This script produces the same effect, but conserves space, which is quite useful if there are many costumes being displayed.
Another way to animate is by using operator blocks. If you want sprites to move, say something, then move again, this way is very effective.
repeat until ((costume#) = (5)) next costume wait (0.05) secs end
This script says to animate the sprite until a certain costume. This allows the sprite to move, stop, talk, then move again.
Animation Delays
Often costumes may change so quickly that the desired animation is faster than wanted. Animations can be slowed by adding the Wait () Secs into the animation script. You will want to change the wait time according to how big the changes in each frame. If the changes are minor, you want a smaller wait. If they are choppy, you would want a bigger wait.
An inefficient animation script with delays:
switch costume to [Stab1 v] wait (0.1) secs switch costume to [Stab2 v] wait (0.1) secs switch costume to [Stab3 v] wait (0.1) secs switch costume to [Stab4 v] wait (0.1) secs
An efficient animation script with delays:
switch costume to [Stab1 v] repeat (5) wait (0.1) secs next costume end
The number in the Repeat () block is the amount of costumes that you'll be going through, not counting the first one that is before the block.
In some animations, the animation delay may have to change as the animation is running. Here, it gets a bit harder with an efficient script. With the inefficient script, it is easy; the multiple delay blocks simply have to be adjusted correctly:
switch costume to [Strike1 v] wait (0.1) secs switch costume to [Strike2 v] wait (0.2) secs switch costume to [Strike3 v] wait (0.3) secs switch costume to [Strike4 v] wait (0.4) secs switch costume to [Strike5 v] wait (0.5) secs
In the efficient script, the second delay block is being used multiple times, and will therefore have to change accordingly.
If the animation delay is repeatedly being changed by the same amount, a variable can be inserted into the delay block and the variable repeatedly changed in the Repeat () block:
set [delay v] to (0.1) switch costume to [Strike1 v] repeat (4) wait (delay) secs next costume change [delay v] by (0.1) end
If the animation delay is changing in an unpredictable way, a list can be used, its items giving the animation delay:
set [item v] to (1) switch costume to [Strike1 v] repeat (4) wait (item (item) of [delays v]) secs next costume change [item v] by (1) end