Building a tower defence game with the turret behaviour

Favourite 94 favourites
Tutorial written by GeometriXOriginally published on 19th, January 2013 - 7 revisions

The following techniques will be covered in this tutorial: creating a grid-based system for placing towers, using the new turret behaviour, using global and local variables, and testing collisions. This tutorial is suitable for beginner Construct 2 users who have at least a working knowledge of the software. This is not a full step-by-step tutorial; I will gloss over simple tasks like creating objects and variables, but I've included a number of hyperlinks to topics that beginners may not be familiar with.

This tutorial is suitable for users with the free version of Construct 2, but I strongly recommend a purchased version that unlocks use of the family function if you plan on developing a larger game with multiple enemy types.

If you'd like to follow along using the completed capx, you can download that here [R116 or later required]. If you'd like to skip straight to setting up the turret behaviour, go to page four and five of this tutorial.

Part 1: Setting up

First we’ll create all of our objects to use in the game. The input objects will be keyboard and mouse. The player-visible objects will be: a basic tower (I’ve called mine an arrow tower; it has the turret behaviour); that tower’s projectile (an arrow; with bullet and destroy outside layout behaviour); the enemy sprite (also with bullet behaviour); a “ready” button (which the player presses to commence the wave); an arrow tower button (which the player clicks on to build a new tower); and separate text objects to show current lives, wave and cash. There’s also a failure text object.

We’ll also create a few objects that will be invisible to the player (you should use cosmetic, non-blocking sprites to show where the player is allowed to build, where the enemy path is and so on). These will be: a grid-system that shows where towers can be placed; an enemy spawner; enemy destinations; and an enemy goal.

Place everything into a layout according to how you’d like your map to be laid out. Place a single instance of the tower, arrow and enemy outside of the layout. The grid should always be offset by 32,32 and be in units of 64 (in this case, at least).

Create global variables called Lives, Timer, CurrentCash, CurrentWave, CurrentEnemies, TotalEnemies, and InHand.

Create an instance variable for the tower object called Floating (boolean - set to true by default); create another variable for the arrow called Damage (number); create variables for the enemy called CurrentDestination and HitPoints (both numbers).


Joannesalfa 28.4k rep

Nice tutorial! :)

Sunday, January 20, 2013 at 5:07:05 AM
SergioRM 6,969 rep

Thank you! And good tutorial!

Sunday, January 20, 2013 at 8:15:42 PM
Gamer 3,345 rep

Such a nice tutorial , I'm a bit confused about the last page though , I was wondering if you could explain a bit more about that turret behavior .

Thursday, January 24, 2013 at 8:15:31 PM
otoduran 2,560 rep

where do i find all the specifics on turret behavior ?
I cant get it to work D=

Sunday, February 03, 2013 at 10:41:09 AM
GeometriX 12.6k rep

I decided to not include the turret behaviour in detail here because there's a pretty good example included with Construct 2 from the R116 release onwards. Looking at the responses, though, I think I'll update this tutorial to cover the behaviour in a little more detail.

Sunday, February 03, 2013 at 12:21:50 PM
Vaupell 2,777 rep

Great tutorial, this is really usefull.

Thank you!

Sunday, February 03, 2013 at 5:12:04 PM
r4dicaldreamer 3,240 rep

cool!! can't wait to try it :)
thanks a lot

Monday, March 04, 2013 at 11:28:08 AM
WaysOfTheSlience 2,057 rep

if you didn't know this but when destroying the tower makes it to where you cant buy it again.

Tuesday, March 19, 2013 at 12:46:58 PM
knalluuk 2,150 rep

Where can you set the Enemy.Count value to the system? Pls tell me I need it!

Saturday, March 30, 2013 at 6:07:33 PM
BluePhaze 14.6k rep

@knalluuk to set the Enemy.Count you need to access the System > Compare two values, then you can choose Enemy.Count (Enemy being the name of the object you want to look at and Count being one of the system variables/properties for it.)

Thursday, April 11, 2013 at 8:43:39 PM
BluePhaze 14.6k rep

@WaysOfTheSlience if that is happening for you then your events are not written correctly. You need to check your on click and make sure it has the Create Object statement in there, and also that you are setting in hand to 0 once you place or destroy a tower.

Thursday, April 11, 2013 at 8:45:18 PM
BluePhaze 14.6k rep

@GeometriX Slight correction to the above info, the rate of fire on the turret is how many seconds between shots, not how many shots per second. For example, setting it to 2 does not mean 2 shots per second, it means it will shoot 1 shot every 2 seconds. To get it faster you need to set it to something less than one. If you want 2 shots per second, set the fire rate to 0.5.

Great tutorial, if you get a chance check your messages as I had a question for you on the pathfinding piece.

Thursday, April 11, 2013 at 9:47:42 PM
zistoire 2,383 rep

There is one problem in your game. The turret starts shooting before it it placed. You should correct it. Apart that it is very good.

Tuesday, April 16, 2013 at 11:20:55 AM
GeometriX 12.6k rep

@zistoire, check the first event on page four, this prevents the turret from acquiring a target while its "isFloating" variable is set to true.

Wednesday, April 24, 2013 at 11:40:54 AM
mrPeanut 1,990 rep

thanks i needed the enemy part for my tower defense. Keep up the good work

Friday, June 07, 2013 at 4:41:58 PM

Leave a comment

Everyone is welcome to leave their thoughts! Register a new account or login.