The good news is that this lesson is going to be very short. What we are going to do is to make the cat walk to the edge of the viewer, turn round and walk back when the space bar is pressed. All we need to do is to add two blocks into the existing code. Here it is in full with the new blocks highlighted:

Coding for Primary Schools

Back to Title

Expanding Animation


Both of these new blocks can be found in the motion category of the scripts tab in the objects window.

The first, set rotation style, can be set to left-right, all round or don't rotate. The cat has no orders of rotational symmetry and we are moving him from left to right and so that's the style we need. If we put don't rotate he'd walk backwards, and all around would turn him sideways or upside down.


The second block is almost self-explanatory and is if on edge, bounce. The cat will turn direction through 180 degrees and end up going the way he came from.


This is the basis for moving sprites horizontally through a game screen and is what you need for a platform or continually moving sprite. Remember that all code has to be attached to the sprite in question.



This is not too satisfactory as we have no control over the direction of the cat at the moment. In a couple of lessons we will make him move in whichever direction we wish by using the keypad arrows but first we will add sound to him when he is clicked by the mouse.


The finished project for this stage is here:





Let's recap on what we've learnt so far in this short bit of code. The one right at the top (when  space key pressed) is called the event handler. You can jam as much code as you like in AFTER an event handler but if you have code without an event handler that code will never run. The next three down are motion blocks. There's lots of things you can do with motion blocks and the three we have here are fundamental to interactive (i.e. user-controlled) motion.


Underneath we have a wait, which as you would expect temporarily stops the script running. If we had any other scripts they'd continue to run, this one only would pause.


Under that we have a conditional. In this case it's an if,,,then....else  (sometimes called if.....else by experienced programmers).

If.....then.....else is used when two states are possible


and when the sprite is in one state we want it in the other. The argument here is if the variable costumeState = 1  and if it is the code under the then part will run, and the variable costumeState will be set to 0 and the costume is switched to costume 2.


If the variable is not  equal to 1 the else runs. The variable gets set to 1 and the costume to costume 1. Such an arrangement is known as a mutual switch or switchover conditional since the variable is mutual to both consequences and the argument.



Piece of cake. I'm sure you'll be delighted to know that another easy lesson is coming. It's adding sound.

Next Lesson >