Making Sprites Move Automatically
If you’re wondering why we left the alien’s movement to the end, it’s because it makes it easier to test the game. We’ve now got a spaceship that the player can move, a working firing mechanism, and an alien that dies and then regenerates when shot. We can test all that at our leisure and fix any problems without worrying about having to keep up with the alien.
Our alien moves from left to right and then from right to left, and then back again. Each time it changes direction, it moves down the screen a little bit. This is quite sophisticated behavior, but you can create most of it using the blocks you’ve already used this chapter. The art of programming is partly about working out how you use the different blocks or commands at your disposal to achieve what you want to.
To start, we need to make a new variable, which we’ll call
leapsize. Each time Scratch goes around the alien’s Forever loop, it moves the sprite and then checks whether it’s touching a fireball or the spaceship. The
leapsize variable is used to store how much the alien’s X position should change by each time. If the alien is going right, the
leapsize variable is
20, and if it’s going left, it’s