Make a Flash game like Flash Element Tower Defense – Part 2

Welcome to the 2nd part of this tutorial. I recommend you to check part 1.

In this tutorial, we will place our first base, and it will start to fire.

Let’s have a look at the objects:

Tower Defense

base: it’s the base

bullet: it’s the bullet the base will shoot

cant_build: it’s the area where you can’t build a base

minion: already explained in part 1.

path: already explained in part 1.

range: it’s the firing range of the base

Now, a little actionscript all in the first frame:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
base_range = 300;
can_be_placed = false;
placed = false;
attachMovie("path", "path", _root.getNextHighestDepth());
attachMovie("cant_build", "cant_build", _root.getNextHighestDepth());
attachMovie("range", "range", _root.getNextHighestDepth(), {_width:base_range, _height:base_range});
attachMovie("base", "base", _root.getNextHighestDepth());
waypoint_x = new Array(40, 140, 140, 220, 220, 80, 80, 340, 340, 420, 420);
waypoint_y = new Array(140, 140, 60, 60, 240, 240, 320, 320, 100, 100, -20);
delay = 25;
new_monster = 0;
monsters_placed = 0;
onEnterFrame = function () {
	if (monsters_placed<25) {
		new_monster++;
	}
	if (new_monster == delay) {
		monsters_placed++;
		new_monster = 0;
		min = attachMovie("minion", "minion"+_root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:40, _y:-20});
		min.point_to_reach = 0;
		min.speed = 1;
		min.onEnterFrame = function() {
			dist_x = waypoint_x[this.point_to_reach]-this._x;
			dist_y = waypoint_y[this.point_to_reach]-this._y;
			if ((Math.abs(dist_x)+Math.abs(dist_y))<1) {
				this.point_to_reach++;
			}
			angle = Math.atan2(dist_y, dist_x);
			this._x = this._x+this.speed*Math.cos(angle);
			this._y = this._y+this.speed*Math.sin(angle);
			this._rotation = angle/Math.PI*180-90;
		};
	}
};
base.onEnterFrame = function() {
	if (!placed) {
		this._x = _root._xmouse;
		this._y = _root._ymouse;
		_root.range._alpha = 100;
		can_be_placed = true;
		if (_root.cant_build.hitTest(this._x-this._width/2, this._y-this._height/2, true) or _root.cant_build.hitTest(this._x+this._width/2, this._y+this._height/2, true) or _root.cant_build.hitTest(this._x+this._width/2, this._y-this._height/2, true) or _root.cant_build.hitTest(this._x-this._width/2, this._y+this._height/2, true)) {
			_root.range._alpha = 0;
			can_be_placed = false;
		}
	}
};
range.onEnterFrame = function() {
	if (!placed) {
		this._x = _root._xmouse;
		this._y = _root._ymouse;
	}
};
onMouseDown = function () {
	if (can_be_placed) {
		placed = true;
	}
};

Line 1: Defining the base range

Line 2: Defining if the base can be placed

Line 3: Defining if the base has been placed

Line 5: Attaching the cant_build movieclip

Line 6: Attaching the range movieclip and setting its width and height to match base_range

Line 7: Attaching the base movieclip

Line 36: Function to be executed for the base at every frame

Line 37: If the base is not already placed…

Lines 38-39: Move the base to the mouse pointer

Line 40: Set the transparency of the range to 100 (fully opaque)

Line 41: Updating can_be_placed to true

Line 42: If a corner of the base is over the cant_build area…

Line 43: Setting the transparency of the range to 0 (fully transparent)

Line 44: Setting can_be_placed to false

What I’ve done: I let the player move the base with the mouse. If the base can’t be placed, because it’s over the walking path, I hide the range to make the player know he is on an area where he can’t build.

Line 48: Function to be executed for the range at every frame

Lines 49-52: Same thing of lines 37-39… if the base has not been placed, then move the range with the mouse

Line 54: Function to be executed when the player clicks the mouse

Line 55: If the base can be placed…

Line 56: Then set placed to true. Now the base is placed

Look: you can now place your base, but only outside the walking path.

Now, we will make the base fire at the foes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
base_range = 300;
can_be_placed = false;
placed = false;
firing = false;
attachMovie("path", "path", _root.getNextHighestDepth());
attachMovie("cant_build", "cant_build", _root.getNextHighestDepth());
attachMovie("range", "range", _root.getNextHighestDepth(), {_width:base_range, _height:base_range});
attachMovie("base", "base", _root.getNextHighestDepth());
waypoint_x = new Array(40, 140, 140, 220, 220, 80, 80, 340, 340, 420, 420);
waypoint_y = new Array(140, 140, 60, 60, 240, 240, 320, 320, 100, 100, -20);
delay = 25;
new_monster = 0;
monsters_placed = 0;
onEnterFrame = function () {
	if (monsters_placed<25) {
		new_monster++;
	}
	if (new_monster == delay) {
		monsters_placed++;
		new_monster = 0;
		min = attachMovie("minion", "minion"+_root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:40, _y:-20});
		min.point_to_reach = 0;
		min.speed = 1;
		min.onEnterFrame = function() {
			dist_x = waypoint_x[this.point_to_reach]-this._x;
			dist_y = waypoint_y[this.point_to_reach]-this._y;
			if ((Math.abs(dist_x)+Math.abs(dist_y))<1) {
				this.point_to_reach++;
			}
			angle = Math.atan2(dist_y, dist_x);
			this._x = this._x+this.speed*Math.cos(angle);
			this._y = this._y+this.speed*Math.sin(angle);
			this._rotation = angle/Math.PI*180-90;
			if (bullet.hitTest(this._x, this._y, true)) {
				firing = false;
				bullet.removeMovieClip();
				this.removeMovieClip();
			}
			if (placed) {
				distance_from_turret_x = base._x-this._x;
				distance_from_turret_y = base._y-this._y;
				if ((Math.sqrt(distance_from_turret_x*distance_from_turret_x+distance_from_turret_y*distance_from_turret_y)<base_range/2) and (!firing)) {
					firing = true;
					bullet = attachMovie("bullet", "bullet"+_root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:base._x, _y:base._y});
					bullet.dir = Math.atan2(distance_from_turret_y, distance_from_turret_x);
					bullet.onEnterFrame = function() {
						this._x -= 3*Math.cos(bullet.dir);
						this._y -= 3*Math.sin(bullet.dir);
						if ((this._x<0) or (this._y<0) or (this._y>400) or (this._x>500)) {
							firing = false;
							this.removeMovieClip();
						}
					};
				}
			}
		};
	}
};
base.onEnterFrame = function() {
	if (!placed) {
		this._x = _root._xmouse;
		this._y = _root._ymouse;
		_root.range._alpha = 100;
		can_be_placed = true;
		if (_root.cant_build.hitTest(this._x-this._width/2, this._y-this._height/2, true) or _root.cant_build.hitTest(this._x+this._width/2, this._y+this._height/2, true) or _root.cant_build.hitTest(this._x+this._width/2, this._y-this._height/2, true) or _root.cant_build.hitTest(this._x-this._width/2, this._y+this._height/2, true)) {
			_root.range._alpha = 0;
			can_be_placed = false;
		}
	}
};
range.onEnterFrame = function() {
	if (!placed) {
		this._x = _root._xmouse;
		this._y = _root._ymouse;
	}
};
onMouseDown = function () {
	if (can_be_placed) {
		placed = true;
	}
};

Line 4: Setting the firing variable to false. Now the base is not firing

Line 34: Performing an hit test between the minion and the bullet. At this time in the script we haven’t already met the bullet, but anyway at this time I am performing this test

Line 35-37: If the test is positive, then remove the bullet and the minion that was hit (at this time minions do not have energy, so my turret is one shot one kill), then set firing to false.

Lines 40-41: Calculating x and y distance between the bullet and the minion

Line 42: If the distance between the bullet and the minion is less than half the range (in our case the range is the diameter of the circle, so half the range is the radius)

Line 43: Boom! The turret fires!

Line 44: Placing the bullet on stage, in the same position of the turret

Line 45: Setting bullet direction, accordind to its x and y distances from the minion

Line 46: Function to be executed for the bullet at every frame

Lines 47-48: Moving the bullet using trigonometry. If you don’t know what is trigonometry, then head to this tutorial

Line 49: Checking if the bullet is flying out of the stage

Lines 50-51: In this case, remove the bullet and set firing to false again to let the turret fire once more

And here it is our firing turret. You may need to reload the page to have the minion walking while you will place the turret and start killing them.

The fire rate sucks a lot, and the same thing does turret’s AI. But I got all those features in only 81 lines, I am sure I will create the complete game in less than 500 lines.

Not today, anyway. Today, download the source and if you have suggestion… just leave a comment.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (196 votes, average: 4.46 out of 5)
Loading ... Loading ...
Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 179 comments

  1. Make a Flash game like Flash Element Tower Defense - Part 1 : Emanuele Feronato - italian geek and PROgrammer

    on November 6, 2007 at 9:37 pm

    [...] 11th update: part 2 [...]

  2. PLEASE HELP

    on November 6, 2007 at 11:37 pm

    PLEASE HELP! i need to press buttons alternately to make the person move, but when
    i try i can hold both and he’ll go really fast.
    if you hold 1 it doesnt move. you want code
    here:
    onClipEvent (enterFrame) {
    if (Key.isDown(Key.LEFT) && this._currentframe == 1) {
    _root.BallOneMin._y -= 1;
    _root.BallOneMax.play();
    _root.BallOneMin.nextFrame();
    } else {
    if (Key.isDown(Key.RIGHT) && this._currentframe == 2) {
    _root.BallOneMin._y -= 1;
    _root.BallOneMax.play();
    _root.BallOneMin.prevFrame();
    }
    }
    }

  3. Joshua threlfall

    on November 7, 2007 at 12:07 am

    for some reason when i try to place turret in above movies it only lets me place it in cant build area.

  4. Matt

    on November 7, 2007 at 4:09 am

    Great tutorial! Your coming out with new stuff almost every day. How do you manage to write so many a tutorials in such a short time?

  5. Emanuele Feronato

    on November 7, 2007 at 11:28 am

    I quit sleeping a year ago

  6. shiv1411

    on November 7, 2007 at 2:15 pm

    hi,
    gr8 tutorial Emanuele.
    Anyways, i completed 5 levels of the game I am going to submit in MochiAds contest. I am planning to make 5 more levels. Shall I send u the complete game too? Whats ur email?

  7. shiv1411

    on November 7, 2007 at 2:21 pm

    hi Emanuele,
    Please make the next part of the claustrophobic SURVIVAL HORROR tut.

  8. Frederik J

    on November 7, 2007 at 3:41 pm

    Emanuele, as always, nice tutorials. To you, it’s just some words, but to us, it’s something… something livechanging. – You help us so much, another time: thank YOU! :)
    /Frederik J

  9. David

    on November 7, 2007 at 6:37 pm

    i have said this many times already i’ve forgotten how to count

    great tutorial

    i can’t wait for the next tutorial on “Create a Flash Racing Game Tutorial”

  10. Frederik J

    on November 7, 2007 at 7:56 pm

    David, thats not Emanuele who writes it, its GameSheep!
    /Frederik J

  11. PLEASE HELP

    on November 7, 2007 at 10:21 pm

    nvrr mind i fixed it =)

  12. conor

    on November 8, 2007 at 10:09 pm

    errmmm love this tut but i got a problem…

    how do ya post in user controbutions??

  13. conor

    on November 8, 2007 at 10:28 pm

    love the tutorial but i have a few quextions i need to ask…

    1. how to post new topics in user contorbutions
    2. can i post a game i made using a gamecore off my uncle and his program that i used to put it al together?

    love a ansewr soon k?
    p.s the game i made is a fps with AI and full functional action kinda like halo or doom but not as good

  14. Emanuele Feronato

    on November 9, 2007 at 3:45 pm

    You can send your works to info@emanueleferonato.com

  15. -

    on November 12, 2007 at 1:26 pm

    GR8! M8! did u quit sleeping? lol then we request u quiting lunch and nature’s call needs to write more tuts!! plzz! ;D

    best regards

  16. jabs

    on November 19, 2007 at 3:15 am

    Nice tut. Reminded me for a few things I need to remember to do when making mine.

  17. Tosho

    on November 20, 2007 at 11:02 pm

    Huge thanks for the tutorial!

    Been looking for something like this for along time. Can’t wait till it is finished!

  18. Thomas

    on November 25, 2007 at 5:14 am

    “powered by coffee”, ay?

  19. Hoi! Koffie? » Tower “Addiction” Defense

    on November 25, 2007 at 2:31 pm

    [...] Make a Flash Game Like Flash Element Tower Defense – Part 2 [...]

  20. Something

    on December 2, 2007 at 3:53 am

    Mine never shoots! plz help

  21. Bluy

    on December 2, 2007 at 11:34 pm

    ok how do i run the fla filess?

  22. Willem

    on December 5, 2007 at 7:11 pm

    I need help dude plleeeeaassee… Nothing works for me, the graphics don’t even show up!
    I really need help as I ALWAYS wanted to build my own tower defence game!

  23. juan

    on December 8, 2007 at 4:06 pm

    What do you need in order to make td’s of your own??? and is it free??

  24. juan

    on December 8, 2007 at 4:06 pm

    What do you need in order to make td’s of your own??? and is it free??

  25. LordCrazyKing

    on December 8, 2007 at 6:26 pm

    The problem with placing the tower is that it checks if any of the corners are on the path and because the tower is wider than the path it can be placed over the path so long as all 4 corners aren’t on the path, might want to look into that for next tutorial or fix it in this one :)

  26. MJGB

    on December 11, 2007 at 6:36 pm

    juan,

    You need to have a copy of Adobe Flash (Or Macromedia Flash) and some knowledge of Actionscript. Or, yo can follow tutorials such as those of Emanuele.

    Can’t wait for the next parts Emanuele Feronato, great blog and tutorials.

  27. Jim

    on December 14, 2007 at 4:55 am

    What is actionscript and how do i get to it

  28. Dakota

    on December 15, 2007 at 8:36 am

    to fix the hittest problem (the 4 corners issue), simply hittest the entire base, not just the corners. or maybe your allowed to place it over the path…

  29. Padfoot02

    on December 15, 2007 at 5:25 pm

    lol if u dont kno wut actionscript is i suggest u find a much simpler tut. (actionscript is the stuff u type in so that the funny pictures do stuff you want them to. its like a type of flash-language.)

  30. Padfoot02

    on December 15, 2007 at 5:28 pm

    also, emanuele, how do you make the rest of the whole game? or are you going to come out with a tut for that later? i didnt catch.

  31. 007

    on December 15, 2007 at 9:35 pm

    Man, real awesome tute, waiting for the final version..like in which you’ll cover the Fire rate and a much better AI

    Thanks

  32. luke

    on December 19, 2007 at 5:32 pm

    Actionscript is what this tutorial is based on retard. you have to use it to code most of the online flash Tower Defences. there is racing and also action ones like Feudalism.

  33. Mark

    on December 21, 2007 at 9:59 pm

    Wow, people are pretty rude on here sometimes and not very informative. For the people asking about this
    1st: You need Adobe Flash to make these games
    (go to adobe.com to get a 30day trial)
    2nd: Actionscript is a programming language that Adobe Flash uses.

    3rd: Hey Emanuele it’s been a long time since ive been here. Your 1st tut on a line rider game inspired me to continue learning flash when i was very doubtful in my skills. Now im thinking about making my own tutorials in flash. So keep up the good work! by the way ill send you my own tower defense game im making, not based on your tut though.

  34. alderman

    on December 24, 2007 at 9:27 pm

    Hi can you tell me how to make so that i could upgrade,build more and turn off or destroy my towers?

    Great Tutorial. Merry Christmas everyone

  35. Joe

    on December 30, 2007 at 4:42 pm

    Hi, can anyone tell me how to start making a tower defense game?
    i’d really appreciate it, thx

  36. Frederik J

    on December 30, 2007 at 6:28 pm

    Joe:
    First of all you need some basic/advanced Actionscript knowledge, if you don’t got it then look at some tutorials. You also need the program Macromedia flash 8 or MX2004 (I recommend 8), and then it is just to start developing the game. You can also follow Emanueles tutorial, that you’ve just read.

  37. Joe

    on December 30, 2007 at 10:08 pm

    I already had flash, how do i open it and start making the game?

  38. Frederik J

    on December 30, 2007 at 10:10 pm

    You just open it, make some movieclips and then start coding.. But it isn’t that simple. You need to know how you code actionscript..

  39. erik

    on January 1, 2008 at 4:24 am

    will emanuelle launch a third part? cause i dont have the knowledge to develop the rest of the game myself

  40. Kenneth

    on January 1, 2008 at 10:49 am

    This is a great tutorial ! Thanks

  41. TheInfinity

    on January 2, 2008 at 8:43 pm

    i can’t open the sources =( It says unexpected file format….Anyone Help?

  42. kasiaijuz

    on January 5, 2008 at 10:19 pm

    yeah,
    i’ve got movie clips and code into actionscript window but how to make it now to work?
    help us!

  43. TheInfinity

    on January 5, 2008 at 10:29 pm

    i built up to this point but when i try it on .swf the minions disappear. Any ideas?

  44. Antony

    on January 6, 2008 at 8:00 pm

    Why, when the speed is adjusted, do the minions flicker and stop in the corners?

  45. mikel

    on January 7, 2008 at 7:12 am

    i have the same problem as you with the tower defence making. how do u fix it?

  46. Sach master

    on January 7, 2008 at 5:33 pm

    I’m 8 :-)

  47. meee

    on January 10, 2008 at 5:35 am

    i put everything in place but when i try to play it it loads then sais “this script is causeing flash to run slowly. if this continues, your computer may become non responsive. do you want to abort?yes/no”
    when i click no it just waits a little and sais the same thing again. how do i fix this?

  48. Jamie

    on January 20, 2008 at 4:35 pm

    hey, this is an awesome tut but im having a problem with the creeps turning, they get to a corner, then flick back and forewards until they are all doing it on top of eachother, when they start going again on top of eachother

    here is the code as it is now (just the turning bit):

    if (new_monster == delay) {
    monsters_placed++;
    new_monster = 0;
    min = attachMovie(“minion”, “minion”+_root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:807.6, _y:252.3});
    min.point_to_reach = 0;
    min.speed = 1;
    min.onEnterFrame = function() {
    dist_x = waypoint_x[_root.min.point_to_reach]-this._x;
    dist_y = waypoint_y[_root.min.point_to_reach]-this._y;
    if ((Math.abs(dist_x)+Math.abs(dist_y))<min.speed) {
    this.point_to_reach++;
    }
    angle = Math.atan2(dist_y, dist_x);
    this._x = this._x+this.speed*Math.cos(angle);
    this._y = this._y+this.speed*Math.sin(angle);
    this._rotation = angle/Math.PI*180-90;

    Please help! thanks, Jamie

  49. LUK

    on January 22, 2008 at 8:33 pm

    Please, release part 3 of this tutorial… It`s great tutorial!

  50. Danny Ocean

    on January 23, 2008 at 4:57 am

    i finished this doing what you said but, when i view it the minions never come. help!

  51. noob

    on January 23, 2008 at 7:40 pm

    i am very noob about making a game so where can i open this side where i can make a minion and pach

  52. Luiz Filipe

    on January 26, 2008 at 10:29 am

    where is the end? part 3?

  53. david

    on January 27, 2008 at 4:39 am

    i dont know how to do the lines it just shows it on my com

  54. david

    on January 27, 2008 at 4:40 am

    click yes trust me

  55. lucas

    on January 30, 2008 at 12:34 am

    i am very excited about making my very own tower
    defense.

  56. Jesse

    on January 30, 2008 at 6:55 pm

    Nice tutorial two questions can you speed up the speed of the bullets and is there a 3rd part to this tutorial yet

  57. td

    on January 31, 2008 at 5:08 am

    When are you going to finish?
    I really want to make a td game!

  58. td

    on January 31, 2008 at 5:10 am

    why did i get modded?

  59. td

    on February 1, 2008 at 10:49 am

    btw i need help making it where you can buy bases.
    when i buy one the background turns white and i can’t place it anywhere,and if i try to buy another one it goes up in the corner.
    any help?

  60. Ice_Hero

    on February 4, 2008 at 9:19 pm

    I got a question where do i get Macromedia Flash
    and how much is it or is it free?

  61. Rc

    on February 5, 2008 at 11:09 pm

    Great tut!!!

    The thing is, I can’t play it… Kinda sad you know :(!!! Please help!!! If you know plese post to reply.

    Thanks,

    Rc

  62. W00T W00T

    on February 18, 2008 at 4:47 pm

    omg i dont got this…

  63. Joar

    on February 20, 2008 at 5:57 pm

    Thanks for the great tutorials. I’m very bussy
    making my own towerdefence, but offcourse i’m stuck now and then.

    Hope you write a 3rd part, i would really appreciate it.

  64. Andy

    on February 21, 2008 at 12:38 am

    How do you call min outside of the min.onEnterFrame= function()
    {
    }
    When I do a hitTest with _root.min inside the bullet’s onEnterFrame, it only works for the min that was most recently created. Is there a way I can program outside the min onEnterFrame and have the code work for all min’s created?

  65. Cory

    on February 25, 2008 at 1:35 am

    O.K. i need the program to make one of these please! Thank you!

  66. L’ultima linea di difesa /4 - Faccio Cose Vedo Gente

    on February 27, 2008 at 12:12 am

    [...] un gioco di difesa è facile: bastano alcune routine per generare i nemici (ma se ne può fare anche a meno agendo direttamente [...]

  67. Draco18s

    on February 28, 2008 at 9:33 am

    I’d really like to see part 3 on this one. I downloaded the source here to expand on and make an actual game, but there are so many things right now that are controlled in actionscript on the main frame, instead of via other controls and it’s a significant amount of work splitting those off without breaking the whole thing.

    I’ve been pondering making a TD game for a while (mainly due to the fact that all the towers out there are the same, and I have a few new ideas to put to the test), but lack the knowhow to figure out how to get started. I really need some starter code and all I have to do is tweak.

  68. ...

    on March 1, 2008 at 8:20 pm

    when are you going to make the third part?? i havent got the brains to make the rest :D

  69. stupid kid w/ dreams

    on March 5, 2008 at 6:07 pm

    how the hell do you even test it? :’(

  70. Mike

    on March 5, 2008 at 7:33 pm

    Great tut. When are you going to get part 3 out?

  71. Mike

    on March 5, 2008 at 7:35 pm

    Great tut. When are you going to get part 3 out? I need some help with the firing. How do i make the bullet never miss or disapear if it leave the range of the tower?

  72. fgfgfdg

    on March 7, 2008 at 10:48 pm

    i love this website i just cant find astral tower defense cause i played it 1 time and then the next day i couldnt find it.

  73. ...

    on March 10, 2008 at 9:11 pm

    PLZ MAKE A THIRD PLZ!!!!

  74. jared

    on March 12, 2008 at 8:15 pm

    according to flash u have errors in lines 4 and 18 dude

  75. jared

    on March 12, 2008 at 8:16 pm

    That was for Jamie the last one

  76. MasterMind

    on March 16, 2008 at 2:17 pm

    Dude, THIS IS AWESOME!!

    3 Q’s

    1 how do you increase the bullet speed?
    2 could you make a part 3 about how to make a building menu where u can click on the base tower (small icon) so u can place it multiple times? (and with money from creeps offcourse)
    3 could you tell me how to make waves? so 1st the triangles and after there are no triangles left, circles etc.

  77. Brandon

    on March 18, 2008 at 1:38 am

    your joking about the non sleeping thing right???

  78. yoded

    on March 24, 2008 at 12:49 pm

    hey mastermind you can make waves by using timers between 2 waves …

    u can try to make a building menu by youreself just create some symbols whit small towers put them in a seperate space on youre frame use a release function so you can click them and copy the coding from the frame that makes you place a turret in the first place …

  79. ryan

    on March 24, 2008 at 6:59 pm

    how do i ga the games maker wats it called and where do i download it???

  80. Jaco208

    on April 1, 2008 at 5:51 am

    Put this on the frame, and make the mc “ball”

    /*
    -Put this on the frame
    -Make sure the mc is named “ball”
    */
    speed=1;
    //Specifies the mc’s movement speed in pixels
    onEnterFrame = function () {
    if (!Key.isDown (Key.LEFT)) {
    l = “up”;
    }
    if (Key.isDown (Key.LEFT) && ball._currentframe == 1) {
    if (l == “up”) {
    ball._y -= speed;
    ball.gotoAndStop (2);
    }
    r=”down”
    }
    if (!Key.isDown (Key.RIGHT)) {
    r = “up”;
    }
    if (Key.isDown (Key.RIGHT) && ball._currentframe == 2) {
    if (l == “up”) {
    ball._y -= speed;
    ball.gotoAndStop (1);
    }
    l=”down”;
    }
    };

  81. Rather Cheesy

    on April 4, 2008 at 8:54 pm

    I love you and this site… Not in a homo way =P

    I haven’t been able to find tutorials like these anywhere else but here. I’m going to start work on a more in depth tower defense game after this by combining your code with codes I already know. I’ll post a link to it once its done if I can remember. ^_^

    This site is epic and I demand that it stay open for eternity!

  82. Zync

    on April 8, 2008 at 9:57 pm

    Ok Emanuelo this tutorial is awsome but you didn’t tell what programm did you use? please post more details because I want to enjoy making a TD game too…

  83. Chris

    on April 9, 2008 at 12:50 am

    I’m trying to load the builder but cannot. Can you help???

  84. John

    on April 14, 2008 at 3:19 am

    I would also like to know what program you used so I also can have fun making my own TD game.

  85. Cleverclogs

    on April 16, 2008 at 9:54 am

    Hi in response to your question, “What program is he using” he is using flash. You can download a free 30 day trial from:

    http://www.adobe.com/products/flash/

    Check out my site -

    http://www.kongregate.com/accounts/cleverclogs

    Cleverclogs =)

  86. digitalt skapande » Tower Defence

    on April 21, 2008 at 2:49 pm

    [...] Part2: http://www.emanueleferonato.com/2007/11/06/make-a-flash-game-like-flash-element-tower-defense-part-2... [...]

  87. Scott

    on April 22, 2008 at 6:19 pm

    im so confused

  88. Zackorith

    on April 26, 2008 at 9:48 pm

    how to make you lose lives if they get to middle!?

  89. help

    on April 29, 2008 at 5:43 am

    what program do you use

  90. Eric

    on May 2, 2008 at 11:57 pm

    Please make a part 3 including:

    -Waves, or levels, of enemies.
    -How to create more towers by clicking and dragging from an icon of that tower.
    -Incorperate money, enemy health, and lives.
    -The ability to select a tower and see its stats.

    Thanks! Awesome tutorial!!

  91. armoured soul

    on May 7, 2008 at 8:56 pm

    ok im new to flash and i put the actions script in and made each symbol in a movie clip exaclty how the tutorial shows im just wondering if anything needs to be on the stage? all i have is the path and nothing is happening and when i drag the minions on nothing happens. any help would be greatly appreciated.

  92. bud

    on May 13, 2008 at 6:29 pm

    help plz not workin

  93. onlineflash

    on May 13, 2008 at 7:37 pm

    i want the next part

  94. onlineflash

    on May 21, 2008 at 10:02 pm

    whens the next one comming out?

  95. oliver_l1

    on May 23, 2008 at 10:42 pm

    Wow! Really amazing. Please make a third part if possible.

  96. atrueguy4u

    on June 4, 2008 at 5:52 pm

    i have an idea for a flash elemet 3 but i really really think it should go to more then just the computer ….. mabey have a chance on systems like the ds or wii or something that would be awsome.

  97. infur

    on June 5, 2008 at 4:25 pm

    please make a third part if possible.

  98. nathan & alex

    on June 7, 2008 at 3:38 am

    I wish i could make a game as cool as urs

  99. atrueguy4u

    on June 9, 2008 at 8:15 pm

    yes who ever made this definitely should make a part 3 but add lots more towers and enemies so it blows us away

  100. td

    on June 12, 2008 at 1:30 am

    how do start making it

  101. td

    on June 12, 2008 at 1:34 am

    how do start making it in what place

  102. markus

    on June 14, 2008 at 10:24 pm

    hi, nice work dude. i really want to make a TD game. but i’m not sure how to do :S. u can e-mail me if u want. cya around :D

  103. markus

    on June 14, 2008 at 10:27 pm

    oh btw, i have made some changes on the suff u did. if your intrested mail me and ill send it to ya. that TD i made would rock if i got it nice and ready “/

  104. bl3h

    on June 17, 2008 at 9:10 am

    markus teach me pls..

  105. random

    on June 18, 2008 at 10:13 am

    ok dude go take a week long nap and then make a part 3.also if any body is proficient in flash/actionsctirpt and wants to work together on a game contact me at electricbrock@gmail.com

  106. temp

    on June 21, 2008 at 5:10 am

    hey go play new game of mine (way cool)

  107. dylmani555

    on June 21, 2008 at 3:55 pm

    Does anyone know where i can get free online actionscript lessons cos me and my mates really want to make this HUGE flash game where you basicaly live out your life a bit like sims but bigger goryer and better graphics

  108. marre2795

    on June 21, 2008 at 11:05 pm

    I can’t open it because it is an unexpected file format

  109. Gamerz

    on June 24, 2008 at 7:12 pm

    Can you get a grid for Macromedia Flash MX? And my syntax doesnt work very well… Tips? Comments?

  110. Atarsh

    on June 25, 2008 at 9:43 pm

    Hey Emanuele,
    I had to make a Tower Defence game for school lately and I based on your tutorial here to create it – though I had to write it AS3, so I actualy had to think a little myself :)
    My game is embedded in Facebook, it uses pictures of your contacts there. You can see it here: http://apps.facebook.com/cliqster/index.php?vis=towerdefence (if you don’t see the game try looking for it on the lower right corner).
    So thanks :) and enjoy.

  111. kid programmer

    on June 27, 2008 at 4:02 am

    did you really quit sleeping?

  112. seth

    on June 27, 2008 at 9:01 pm

    i don’t know how to anything.Do you know how?

  113. seth

    on June 27, 2008 at 9:03 pm

    I have NO idea.

  114. kid programmer

    on June 28, 2008 at 9:52 pm

    Are you new to flash? You should use a flash program 6, 7, or, 8. you simply copy the programming and place it into “frame actions” which can be acessed by right clicking a frame and selecting “actions” from the list. you then past the code into the box and there you go. The programming also requires you to name the things in the library the same as in this tutorial otherwise it won’t work. You can also modify the actionscript so you can make up your own names. you also need the same path or again modify the
    waypoint_x = new Array (modify the numbers) and do the same thing with “waypoint_y” You can find the coordinates through “mouse coordinates” by going to windows at the top, selecting panels and selecting info from that.

  115. Ben

    on June 29, 2008 at 8:36 am

    Hi im new here and im wondering where to put this stuff in? Also i don’t even kno wat the program is, wat is the program to make these and where do u paste it!

    Cheers plz reply

    My email to tell me: battleonkid1@hotmail.com

  116. name

    on July 12, 2008 at 4:35 am

    i need a free(not free so many-day trial)action script program for windows xp professional.

  117. ok

    on July 28, 2008 at 3:52 pm

    ok, but how do you do this with macromedia flash 8? i am a total begginer.

  118. iK

    on August 8, 2008 at 9:11 pm

    Hey I just stumbled across this. I found this ver insightful and my AI works and more. However the isse I am having is getting my characters to Steers around corners and not the zig-zag effect. Any pointers.

    You ActionScript logic is very nice thanks.

  119. anonamus responder

    on August 11, 2008 at 3:38 pm

    that looks complicated!!!!!!!!!!!!!!!

  120. Shedokan

    on August 20, 2008 at 11:34 pm

    How can I make it work with a lot of towers?

  121. Adobe Flash CS3- AS3 and AS2 game tutorials roundup | Lemlinh.com

    on August 28, 2008 at 1:57 am

    [...] Part 2 [...]

  122. Alexius Lengele

    on September 1, 2008 at 12:24 am

    I’m making a similar game for a class of mine and I was wondering if you had any tips to make it simple but still work nicely…

  123. Vishal

    on September 1, 2008 at 6:26 pm

    Hey i am trying to make a TD game like this for a project but am confused please can you help me?

  124. cody

    on September 9, 2008 at 12:52 am

    wat program to make one of these?

  125. Josh

    on September 9, 2008 at 10:45 pm

    come on schools started again and you still havnt updated this tut lol its my fav so i would really like a better turret ai or a link to a site with a good script for it im kinda stuck right now

  126. Smurf

    on September 15, 2008 at 7:10 pm

    I’ve been using this instead of the ‘hitTest’ of the bullet

    if (bullet._x this._x-10 && bullet._y > this._y-10 && bullet._y < this._y+10){
    firing = false;
    bullet.removeMovieClip();
    this.removeMovieClip();
    }

    so it makes it somewhat easier for the tower to actually hit a monster :P

  127. emil

    on October 8, 2008 at 7:04 pm

    hey what program do u use you didnt say that…

  128. emil

    on October 9, 2008 at 4:04 pm

    how do i play it when i opened it with macromedia flash pro?

  129. Didds

    on October 11, 2008 at 9:59 pm

    @Smurf

    Your code doesn’t work

  130. rodneykzf

    on October 13, 2008 at 3:34 pm

    Hi,

    I can’t find the software to use this code.
    PLEASE HELP

  131. george

    on October 17, 2008 at 10:13 pm

    ok……
    what program do u use to make a td game like this???

  132. DudewithKnivesinhisPants

    on November 29, 2008 at 6:48 pm

    You guys asking what program to use, you are all morons.
    “Make a –>FLASHFLASH<– Element Tower Defence – Part 2″

    The clue is there, in the NAME!
    learn to read before posting retarded comments.

    Jees.

    Nice tut man, can’t wait for part three! ;)

  133. Walter Reid

    on December 10, 2008 at 3:58 pm

    Just to let people know I’ve been build a tutorial series like this off my blog – I will encompass all the items covered here and then some. Stop on by!

    http://walterreid.com

    The link to the flash is off my front page or can be searched with the keywords “How to build a tower defense flash game”

  134. bitz

    on December 12, 2008 at 1:51 pm

    i don’t now how to make a game can you tach me how to make a game

  135. slym

    on December 18, 2008 at 3:48 am

    When is part 3 comming out?

  136. anonymous

    on December 19, 2008 at 5:38 am

    uughhh need part 3…

  137. Rasputen

    on December 22, 2008 at 8:26 am

    I modified the code to place multiple towers and to allow the towers to fire on a delay counter instead of waiting until the bullet is removed to fire another. I have used the same variables names with the suffix “Array”. Now, I am stuck on the direction. I can’t find a way to set the direction so that different bullets aren’t all updated to travel at the same angle. I tried using the direction as it’s own array, but can’t set the ID in the bullet.OnEnterFrame function.

    Actually in most of my current projects, I have that problem. How do I have an onEnterFrame function recognize a value only intended for one instance of that function. Can anyone please help me out? I would really appreciate it. I am exhausted from all the Tutorials and forums in hunt of this.

    (sorry for the long post, now lets hope everything paste correctly) here’s the code

    for (i=0;i<towerArray.length;i++)
    {
    firingArray[i] -= 1;
    if (placedArray[i])
    {
    distance_from_turret_x = towerArray[i]._x-this._x;
    distance_from_turret_y = towerArray[i]._y-this._y;
    if ((Math.sqrt(distance_from_turret_x*distance_from_turret_x+distance_from_turret_y*distance_from_turret_y)<base_range/2) and (firingArray[i] <= 0))
    {
    firingArray[i] = firingDelay;
    bullet = attachMovie(“bullet”, “bullet”+_root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:towerArray[i]._x, _y:towerArray[i]._y});
    bulletArray.push(bullet);
    bullet.dir = Math.atan2(distance_from_turret_y, distance_from_turret_x);//here(?)

    }

    }

    bullet.onEnterFrame = function()
    {
    this._x -= 1*Math.cos(bullet.dir);//here(?)
    this._y -= 1*Math.sin(bullet.dir);//here(?)
    if ((this._x<0) or (this._y400) or (this._x>500))
    {
    this.removeMovieClip();
    }
    };

  138. rasputen

    on December 24, 2008 at 3:59 am

    I fixed the above code by changing bullet.dir to this.dir but it only works when the bullet.onEnterFrame is embedded in the main onEnterFrame function. Any help?

  139. Bryce

    on December 30, 2008 at 11:34 pm

    how do u do this i cant get to the part where u make a road or minions??????????????????

  140. mat

    on January 12, 2009 at 5:41 pm

    thx a lot man what do you need do download and where from

  141. bear

    on January 17, 2009 at 6:09 pm

    a new TD and i hope you like it

  142. Nyny

    on January 20, 2009 at 11:27 pm

    What language is used for this TD game?

  143. Make

    on January 22, 2009 at 9:17 am

    PART 3!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  144. Kat

    on January 27, 2009 at 9:48 pm

    Another person begging for a part three… Also, if you could clear up a couple things. The range and base both line up the top left corner instead of the middle, how do I fix this? And for the cant_build, do you draw that out in flash or use an imported image of the path? I tried importing the path I was using but it comes out as a rectangle. Is there a way to set it to ignore the transparent parts or do I just have to draw it out in Flash? (I’m using .png files for graphics.)

    Last question for now… Know of any books or websites that could give me a good introduction to actionscript? I’m already reading everything I can get my hands on, but it’s difficult to find references for beginners, and a lot of the basics are geared towards presentations or web design rather than games.

  145. Flash Tower Defence Prototype v 0.9 | Its Not Another Gaming Site

    on February 5, 2009 at 8:01 am

    [...] I was playing around with Emanuele Feronato’s Tower defence prototype, which I found at his great site here [...]

  146. Sunil Changrani

    on February 5, 2009 at 8:03 am

    Hi guys,

    I have been waiting for part 3 for a long time too, but anyways I decided to play around with the source file Emanuele’s given here and came up with an almost complete prototype.
    You can check it out here
    http://www.itsnags.com/archives/240
    It features multiple towers
    Multiple waves of monsters
    Monsters getting tougher on each wave

    I need some more time to complete it and then I’ll be posting a tutorial.
    Thanks to Emanuele for his wonderful post!

  147. Newbie

    on March 5, 2009 at 11:07 pm

    I’ve tried to let the bullet (an arrow) rotate to the minion.
    But I couldn’t figure out how.
    please help

  148. skyler

    on April 9, 2009 at 2:36 pm

    Please make a part 3 so people like me (really beginner at flash) can finish the game. Some good ideas would be money system, upgradable towers, selling towers, different types of towers.

    Thanks for the Tutorial!

  149. Jjjet42

    on May 21, 2009 at 6:52 pm

    I though i’d let everyone know that i know a better tutorial for this. Just search for
    Walter Reid Tower Defence tutorial.

  150. Apsea

    on June 10, 2009 at 6:08 am

    This is amazing. I am a tower defence creator but I only do it on paper and on Microsoft PowerPoint. there is 1 question.
    What progran do you use?
    Thanks.
    And come on my website!

    Http//:www.freewebs.com/Clubarv/

  151. Apsea

    on June 10, 2009 at 6:10 am

    Hey also I want a part 3!

    Please

    Ap

  152. Unknown

    on June 20, 2009 at 7:41 pm

    Apensea – Adobe Flash. Assuming that you aren’t a teacher or a student, it would cost about $750 to buy the program.

  153. Jon

    on July 23, 2009 at 6:15 am

    Hey im just wondering (im kinda new to this) would Adobe Flash Player 10 be a reliable program to build Tower Defense games? Thanks

  154. bob

    on August 20, 2009 at 4:04 am

    suuup freakin sweet tuts but like wat program you use?? i kno it says Flash but can you email me a more specific thing? and maybe give me a link to where i can get it? plzzzzzz

  155. bob

    on August 20, 2009 at 4:05 am

    my email is Coolmaster55@yahoo.com

  156. dalton

    on August 29, 2009 at 5:17 am

    i need help. i downloaded this thing and now i havent the slightest clue wat to do now. i dont even have a screen or anything just the thing is downloaded. this thing really doesnt explain that much at all, like wat to do RIGHT after it just assumes u know.

  157. NEAR

    on September 25, 2009 at 4:35 pm

    Just wanan say this is a great site, me and my friends are forming a programming team and Walter Reid, your site is very helpfu!

    So I have this idea, in order to make the firing rate quicker, we need bullets fast, no? so how about a command that says “hey, you, bullet, yeah you, when you go outside the firing range, leav the clip so we can sand another one of our boys!” unfortunately I don’t know how to do this, but I have a theory:

    if(_root.bullet. hitTest_range = false, remove clip)

    sometihng like that….
    Also, for bullet speed, stick this in somewhere

    bullet.speed = 0;

    tweak these a bit and you may come up with sometihng good, if you really needed this and it helped you, I’m happy to help!

  158. Chris

    on November 6, 2009 at 8:22 pm

    Can you please finsh the tutorial and show us how to make weapons and make them fire.

  159. kamal98

    on January 6, 2010 at 8:04 pm

    This is wonderful i am loving it.

  160. dani

    on February 23, 2010 at 2:56 pm

    oigan yo tengo el flash 5 y puedo crear juegos pero me salen mal los de plataforma y tower defence

  161. Kevin

    on May 14, 2010 at 3:03 am

    Wow, cool tutorial :D

  162. Paisakamana

    on June 6, 2010 at 1:16 am

    Great tutorial but a simple game. I would like to see something more advanced :)

  163. nino

    on June 16, 2010 at 10:27 pm

    i want to make so bad thanks but this is my moms work computer so i cant download stuff thanks any way

  164. marciorosa.org » Make a Flash game like Flash Element Tower Defense – Part 1 - Complete listing Tools and resorces for the ultimate web developer

    on September 9, 2010 at 4:25 am

    [...] start showing the objects required to make it work , rewrite the code you can find in the original post by Emanuele Feronato. Download the source code and give me feedback by Emanuele [...]

  165. marciorosa.org » Make a Flash game like Flash Element Tower Defense – Part 2 - Complete listing Tools and resorces for the ultimate web developer

    on September 9, 2010 at 4:28 am

    [...] Not today, anyway. Today, download the source and if you have suggestion… just leave a comment, to rewrite the code you can find in the original post. [...]

  166. KiviBot

    on September 19, 2010 at 4:57 pm

    This script has a glich:
    you can place that tower to path

  167. jhulius

    on December 1, 2010 at 12:15 am

    who know how to make loading in flash 5?

  168. SeVeReD ZaLeN

    on December 21, 2010 at 9:14 pm

    u need 2 make a third tut!!!!!!!!!!!!!!!!!!!!!!!!!
    plz

  169. Taasen

    on December 31, 2010 at 12:57 am

    i dont really understand how to use the scripts, do you have a program i can download or, do you do something else… please respond :D

  170. Dave

    on January 10, 2011 at 3:39 pm

    Hello,
    Why if I put the speeds higher, the minions will crash in the corners, how can i help that?

  171. Jopz

    on February 2, 2011 at 3:33 pm

    you should atleast finish a game if you start doing tutorials for it right? its been a long time, you should finish this ~__~

  172. nyj

    on February 18, 2011 at 2:48 pm

    can u send to me ur works in this Flash TD tut because i have some problem. can un help me? sory poor english.

  173. nyj_25

    on February 18, 2011 at 3:02 pm

    sir can u help me? in this tut. i cant get the script. this my email nyj_25@yahoo.com

  174. Suman Acharya

    on April 1, 2011 at 5:56 pm

    good tutorial. i’m going to create a great game using this..Thanks

  175. Mike Hunt

    on April 28, 2011 at 12:53 pm

    This is a great tutorial. I told my friend Ben Dover to have a go at this. He thought it was great too. Thanks again for your great tutorial.

  176. Brendon Ray

    on May 13, 2011 at 6:40 am

    First i would like to thank you for all the work you do. I am currently working on a project for school and I am trying to make a tower defence game. The problem is I cant find any code for actionscript that allows me to make the tower shoot at a specific target or allow the user to choose the tower’s primary target (first, last, least health, etc.) Also I am having trouble finding out how to make a money system, life system, and how to give enemies stats like health and speed.

  177. gavin

    on May 16, 2011 at 11:07 pm

    ok im probably an idiot for even trying to make something like this but where do you get all the codes for these games. I am just trying to make one to see what its like but it likes mostly like random words. If anyone could tell me where to go to find it that would be great

  178. xxxxxxxxxxxxxxxxxx

    on May 30, 2011 at 2:28 pm

    It doesn’t work. all i get is the damn minions but no tower

  179. Ayon

    on September 23, 2011 at 9:20 am

    Man,you game sucks.cuz there is only one turret and no game overs or wins or
    money or upgrading………….