Setting up the turrets

Now that we have a level, we need to add a turret. Double-click on the background, select add sprite, and then add the graphic. Make sure that the dimension of this graphic is 32 by 32; everything that we had set up earlier needs to be on this grid. Your playing area should look like the following screenshot:

Setting up the turrets

First, we will add some functionality using the following steps:

  1. Add a Touch object from the Insert New Object window.
  2. Then, go to your event sheet and add an event. This event is going to be a Touch event. Remember that touch events and mouse clicks are the same.

    Tip

    It's better to set up the touch event for simple clicks. Now, people on mobile devices can play your game.

    Setting up the turrets
  3. When we touch the black area, a turret is spawned. This will spawn a turret on the screen. Add an On touched object event, as shown in the following screenshot:
    Setting up the turrets
  4. The object we want to be touched is the PlaceTurret object, as shown in the following screenshot:
    Setting up the turrets
  5. Click on the Done button and your project should look like the following screenshot:
    Setting up the turrets
  6. Then, click on Add action and add a System action.
  7. Select the Create object action, as shown in the following screenshot:
    Setting up the turrets

We want to create a turret object on layer 1. Remember that layer 1 is the player layer—if we spawn it on layer 0, it will be beneath the player layer and we would not be able to see the turrets. We also need to add some code. This will make sure that the turret snaps to the grid. As you can see in the following screenshot, the numbers are related to 32, which is our grid size:

Setting up the turrets

For the next part, we need to do a little bit of math. Doing so will snap the turret to the grid. The numbers represent a grid of 32 by 32. We are adding X and Y values because we need to snap to an X and Y position. You need to add the following code to your project:

round((Touch.X-16 /32) * 32 + 16)
round((Touch.Y-16 /32) * 32 + 16)

Your project should have the following elements:

Setting up the turrets

Now, it's time to test our game. Press F5 and then try it out. It should look something like the following screenshot:

Setting up the turrets
..................Content has been hidden....................

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