Animating a Sprite

Материал из Поле цифровой дидактики

Animating a sprite is the process of a sprite repeatedly changes costumes, creating an animation. Animation scripts vary, depending on the number of costumes and speed.

Creating an Animating Script

The simplest way to animate a sprite is to repeatedly use the switch costume to () and wait () seconds blocks:

switch costume to [Slash1 v]
wait (0.05) seconds
switch costume to [Slash2 v]
wait (0.05) seconds
switch costume to [Slash3 v]
wait (0.05) seconds
switch costume to [Slash4 v]
wait (0.05) seconds
switch costume to [Slash5 v]
wait (0.05) seconds
switch costume to [Slash6 v]

This, however, is lengthy and redundant. An alternative method involves iteration:

switch costume to [Slash1 v]
repeat (5)
    wait (0.05) seconds
    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. This method allows for more control over the animation.

repeat until <(costume [number v] :: looks) = (5) >
    next costume
    wait (0.05) seconds
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

Sometimes, costumes may change quickly enough that the animation produced is faster than desired. Animations can be slowed by adding the Wait () Seconds block into the animation script. The wait time is changed according to the frame rate, with more frames having small wait times.

An inefficient animation script with delays:

switch costume to [Stab1 v]
wait (0.1) seconds
switch costume to [Stab2 v]
wait (0.1) seconds
switch costume to [Stab3 v]
wait (0.1) seconds
switch costume to [Stab4 v]
wait (0.1) seconds

An efficient animation script with delays:

switch costume to [Stab1 v]
repeat (5)
   wait (0.1) seconds
   next costume
end

The number in the Repeat () block is set to the amount of costumes that will be used, 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. With the expanded script, it is easy; the multiple delay blocks simply have to be adjusted correctly:

switch costume to [Strike1 v]
wait (0.1) seconds
switch costume to [Strike2 v]
wait (0.2) seconds
switch costume to [Strike3 v]
wait (0.3) seconds
switch costume to [Strike4 v]
wait (0.4) seconds
switch costume to [Strike5 v]
wait (0.5) seconds

In the compact 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 repeatedly changed in the Repeat () block:

set [delay v] to (0.1)
switch costume to [Strike1 v]
repeat (4)
    wait (delay) seconds
    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

See Also

de:Figuren animieren fr:Scratch Wiki Accueil/tutos/016 animation lutin