Building a tower defence game with the turret behaviour

Index

Stats

11,845 visits, 44,795 views

Tools

Part 2: The towers

Now we’re going to create four events to handle the placement of the tower.

The first event will generate a new tower when we click on the build tower button or press the corresponding hotkey (A, in this case). As you can see I’ve split the event into two parts: the first tests to see if the key is pressed OR if the mouse is clicked on the button; if either happens, then Construct 2 will jump to the second part to test if the player’s current cash is over $30 – the cost I’ve assigned to this tower. There’s also a condition that tests if InHand is currently set to 0. If all of this happens, then it’ll create a new tower instance at the player’s mouse, which we will say is “floating” until it’s been placed down. We also set InHand to 1, to prevent the player from “picking up” more than one tower at a time.

Next, we want to set the towers to always snap to a grid. I’ve set my towers to be 64x64 pixels, so that’s the size of the grid. Create an event that tests to see if the tower is floating (i.e.: if Floating is true). This will automatically test that condition every tick. Create an action that adjusts the tower’s position as below. The math is pretty simple and should make sense based on our afore-mentioned tower size.

Our third event will be called when the player clicks the mouse, and we need to test if the current tower in-hand is within the placement grid, that it’s not overlapping another tower, and that it’s currently floating. Once we’re happy with all of that, we can set the tower’s Floating status to false (which stops it from being moved every tick as in the previous event), take $30 from the player, and set InHand back to zero to allow the player to pick up another tower if they wish.

Our final event is simple: if the player right-clicks while a tower is floating, then destroy that tower to cancel the build order.

  • 1 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • I cant seem to get the turret bullets to actually hit the enemies..? ive tried changing speed of enemies and projectile speed but there are still plenty times the bullet will miss. any tips?