In other languages

Help the wiki!

The Scratch Wiki is made by and for Scratchers. Do you want to contribute?

Learn more about joining as an editor!

See discussions in the Community Portal

Animating a Sprite

Animating a sprite is where 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:

switch costume to [Slash1 v]
switch costume to [Slash2 v]
switch costume to [Slash3 v]
switch costume to [Slash4 v]
switch costume to [Slash5 v]
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)
   next costume
end

This script produces the same effect, but conserves space, which is quite useful if there are many costumes being displayed.

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 to costume [Stab1 v]
wait (0.1) secs
switch to costume [Stab2 v]
wait (0.1) secs
switch to costume [Stab3 v]
wait (0.1) secs
switch to costume [Stab4 v]
wait (0.1) secs

An efficient animation script with delays:

switch to costume [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 to costume [Strike1 v]
wait (0.1) secs
switch to costume [Strike2 v]
wait (0.2) secs
switch to costume [Strike3 v]
wait (0.3) secs
switch to costume [Strike4 v]
wait (0.4) secs
switch to costume [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 to costume [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 to costume [Strike1 v]
repeat (4)
   wait (item (item) of [delays v]) secs
   next costume
   change [item v] by (1)
end

See Also

  • This page was last modified on 11 February 2014, at 04:30.