4. Spawning Enemies
In this screencast, you will program the game to spawn enemies that bounce around the screen.
This screencast will show you how to complete the programming, then you’ll get to try it for yourself. First, you need to add and resize an enemy.
The example project will use a shark, but you can select any sprite you like.
Now that the sprite is added, you need to program it to bounce around the screen.
Remember how you did this for the escape game on day 5?
First, And finally, you added a flag event to start the motion.
Great! Remember, computer scientists like you often break down a problem into smaller, more manageable pieces and solve them one by one.
That method makes even the largest problems easier to solve.
Now that the sprite is bouncing around the screen, you will need to clone, or spawn, new enemies.
In previous games, you may have manually copied a sprite.
But, one of the great things about computer science is that you can program the computer to do things for you. While you could manually right click to copy a new sprite each time you wanted to make the game harder, it’s much easier to get the computer to do it for you with a “clone” block. To start, try cloning a sprite once.
Do this by dragging out a “create a clone of myself” block and a “when I start as a clone” block. Even though the “when I start as a clone” block is in control, you may recognize by its shape and the word “when” that this is a type of event.
Drag the code you previously created onto the “when I start as a clone” event, and drag the “create clone of myself” block to the flag event.
Now this reads: Test it now and watch what happens.
Awesome! When the flag is clicked, a clone starts, but you’ll notice that the other sprite doesn’t do anything. You’ll fix that in a minute. First, test how the “clone” block works with a “repeat” block. Go to control, drag out a “repeat 10” block, and place it around the “create clone of myself” block.
What happens when you test it?
When this loop runs now, the computer should create 10 clones of the enemy sprite!
Good! In the next screencast, you'll program the sprites to clone only if a point is scored.
There is still a bug in this program. There is a sprite on the screen that doesn’t move!
This happens because only the clones are programmed to move.
There are a few ways to fix this, but an easy fix is to hide the sprite that isn’t moving, and show only the sprites that are cloned.
To do that, add a “hide” block to the end of your flag event.
This will hide the first sprite, but it will also hide all of the clones. Add a “show” block to the “when I start as a clone” event so all the clones will show up.
Now all of your clones move!