🏠
Section 1.

Animation

At present your sprite just slides across the screen.
It would be more realistic if it walked.
Many Scratch sprites have costumes to do this.
Let's learn how to use them.

For example, the 'Avery Walking' sprite comes with four walking costumes.
The costumes show her walking to the right. (There is another Avery sprite where she is just standing still.)

Section 2.

The Plan

We will start with the first costume.
We keep calling the 'next costume' block.
Until we get to the last costume.
Then we use the 'switch costume to ...' block
to go back to the first costume.

Section 3.

Coding

Costumes have names and numbers.
In our code we will use the costume numbers.

We will create a new block, called 'walk'.
It will take three inputs:

  1. speed: this is the sprite's speed.
  2. first: this is the first costume we want to use.
  3. last: this is the last costume we want to use.
Of course, at the moment we only have four costumes!
So 'first' will be 1 and 'last' will be 4.
Later on we will have four more costumes.
These will show Avery walking to the left.

We start off by checking that speed is not zero.
If it is zero, there is nothing to do.

Section 4.

Next we need an if - else - block.

In the if part we want to do the same thing ('next costume')
for all the costume numbers from 'first' to the one before 'last'.

Think about this:
Something would go wrong if we used this code:
'first' < (costume number) and (costume number) < 'last'
What do you think would be wrong?

We would get stuck on the first costume!
This is because when the costume number is 'first'
the 'if' part isn't true, so the 'else' code runs.
Which sets the costume number to 'first'.
So this is all that ever happens.

To fix this we use 'first - 1' < 'costume number'.
Now when the first costume is used it moves on to the next costume.
Just what we wanted.

In the else part we want to switch the costume to the first one.

We need to use the wait block, or the costumes will change too quickly.

Section 5.

Using the Speed input

I will now explain the code that uses the speed input.

We need the costumes to change slowly when the sprite is walking slowly.
We need the costumes to change more quickly when the sprite speeds up.

So the input to the wait block depends on the 'speed' input.

We call the 'walk' block after we have called the 'change speed x' block.
So the smallest speed the sprite will be going is the minimum speed x.
We set this to 3 in our setup block.
For this smallest speed, we need a slow speed for the costume changes:
a wait of 1 / 3 of a second is just right.

So we might ask ourselves, would 1 / speed x always work?

It starts at the right value.
What happens as speed x gets bigger?
1 / speed x gets smaller!
Which means that the wait is shorter.
So the costume changes happen faster.

This is just what we wanted.
That's why the code uses 1 / speed in the wait block.


Section 6.

Division by Zero

Our code now uses a division sum to work out how long to wait.
You need to be very careful with division sums.
The number you are dividing by must not be zero.

Dividing by zero makes no sense.
It can't be done by anyone.
It can't be done by a computer.

If we ask a computer to divide by zero we will get an error message.
A language called python says: "ZeroDivisionError: division by zero".
Scratch says that the result is "Infinity".
Its not really true but it does tell you that this is not a sensible sum to do.

So, lets get in the habit of checking that the number we are going to divide by is not zero. In this case, because speed is never negative, we test that: speed > 0

Section 7.

Of course, we need to call it in our main program.

When you have created the 'walk' block
add it to your main program.
Then test it out!

Section 10.

Congratulations: if you hold the right arrow key down, your sprite:

We will use the same code to walk to the left.

Section 1.

Animation, Part 2.

At present your sprite can walk to the right.
We want to be able to go left too!
It's easy to write the code to to do this.

Have you made the costumes that walk to the left?
If you have, carry on with this page.
If you haven't then you need to use this page:
Flipping Costumes

Check that your 'walking left' costumes are numbers 5, 6, 7 and 8.
Then add a call to the walk block as shown in the picture.

Section 2.

Well done!

Your sprite can now walk left and right across the screen.

🏠