Creating an enemy

The next step is to add an enemy for the player to dodge or defeat. We will look for another suitable watery character to continue the underwater theme of this game. We will make some changes to the appearance of the character and then write some movement scripts for the character.

Prepare for lift off

Let's search for a suitable enemy for the diver character using the following steps:

  1. When we look through the library, we can find several interesting water creatures; crab, fish, shark, and starfish. Let's choose the starfish, because it can easily move in different directions without looking weird.
  2. We will change the color of the sprite to be a bit darker than the default bright pink.
  3. Go to the Costumes editor and choose a dark red color.
  4. Make sure that you view the image in Bitmap mode. This will make a big difference to the running speed of the game when we create many starfish. You can toggle this with the buttons in the lower-right corner of the editor.
  5. Select the Fill tool and fill the starfish with the red color. Recolor both costumes just for the sake of completion.
    Prepare for lift off

Engage thrusters

The original sprite will be placed on the right side of the screen. From there, we will use the cloning method to generate waves of starfish moving left towards the player character by following these steps:

  1. Start with a when <green flag> clicked block.
  2. Let's click on hide to make the sprite invisible when not needed.
  3. Also, enter the value 30 in the set size to ()% block to make it smaller. It should be about the same size as the diver.
  4. Add a forever loop to hold the clone generation script.

    We will make the sprite jump to a random location vertically, point in a somewhat random direction to the left, and then spawn five clones.

  5. Set up the starting position using the go to x: () y: () and point in direction () blocks.
  6. The x value will be 240, the right edge of the stage. The y value will be a random value between -180 and 180, the full y range of the stage.
  7. The basic direction of the sprite will be to the left or -90. We will modify that value with a random range. The direction may deviate 30 degrees both ways. So we replace the fixed direction using the pick random () to () block; enter the values-60 and -120 respectively.
  8. We also need to change the sprites rotation style to left to right (the bi-directional arrow) in the sprite Properties panel. This makes sure the starfish isn't swimming upside down.
  9. Next, repeat the creation of the clone five times by entering the value 5 in the repeat() block.
  10. We will obviously include a create clone of <myself> block.
  11. We will enter the value 0.2 in the wait () secs block and wait between each cloning process to put some space between the clones.
  12. After the cloning process, wait for a random time between 1 and 3 seconds by entering the value 1 and 3 in the wait pick random () to () secs block respectively.
  13. Put all steps of the cloning process inside the forever loop to make it run endlessly.

The following screenshot shows the final script:

Engage thrusters

Now we are ready to start moving those clones using the following steps:

  1. Create a second script starting with a when I start as a clone block.
  2. Let's first enable show to make the cloned sprites visible.
  3. The clones' movements will repeat until it touches the edge as seen in the repeat until touching <edge> ? block.
  4. The clone will move 5 steps each cycle (using the move 5 steps block).
  5. When the touching condition is met, we will delete this clone by using the delete this clone block.

Test the game and you will discover that there is a problem. The clones won't show up or even if it does, only briefly. What is happening? Why are they disappearing so quickly? The answer is easy. We just instructed the clones to delete themselves when they are touching an edge. The clones start their life on the right edge of the stage, so they are immediately deleted after creation.

We can fix this problem by adding another loop to the script as shown in the following steps:

  1. Before the repeat until () block, place a repeat () block; enter the value 10.
  2. Inside the block, place a move () steps block; enter the value 5.

This will ensure that the clones will first move 10 x 5 = 50 steps. This gives them enough room to clear the right edge before starting to check for collision with the edge.

Engage thrusters

Objective complete – mini debriefing

The first wave of enemies are now happily flying across the screen. We will add a bit more functionality later. The enemies will be able to kill the player character upon touching him, and likewise, they may be killed by the player if he uses his weapon.

But let's first add a few different enemies.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.142.166