Make a Flash game like Flash Element Tower Defense - Part 1

November 11th update: part 2 released

One of the most popular Flash games around the web during last months was a game (with some variants) called Flash Element TD, "a Macromedia Flash based Warcraft TD game inspired by Element TD for WarcraftIII", as says its author, David Scott.

Flash Element Tower Defense

The game concept is simple: there is a road, and some minions walking that road. You have to kill all minions before they reach the end of the road. You can build several facilities to kill minions and earn money to upgrade weapons as the minions get stronger and faster.

In this first part of the tutorial, I'll cover the minion walking.

It's a very complex task, that will require almost 30 lines!

Let's start showing the objects required to make it work

Tower Defense

minion: it's the evil enemy to kill. I think a blue triangle should look evil enough.

path: it's the background where the game will take place

Now, a few words about how to develop the concept: in this game there is not enemy artificial intelligence. Enemies walk along a path until they die. This simplifies a lot our work, because the only thing we need to do is to create a list of waypoints defining the path. When an enemy reaches a waypoint, I'll make it reach another waypoint.

Let's see the actionscript, all in the first (and only) frame of the main (and only) scene.

ACTIONSCRIPT:
  1. attachMovie("path","path",_root.getNextHighestDepth());
  2. waypoint_x = new Array(40, 140, 140, 220, 220, 80, 80, 340, 340, 420, 420);
  3. waypoint_y = new Array(140, 140, 60, 60, 240, 240, 320, 320, 100, 100, -20);
  4. delay = 25;
  5. new_monster = 0;
  6. monsters_placed = 0;
  7. onEnterFrame = function () {
  8.     if (monsters_placed<25) {
  9.         new_monster++;
  10.     }
  11.     if (new_monster == delay) {
  12.         monsters_placed++;
  13.         new_monster = 0;
  14.         min = attachMovie("minion", "minion"+_root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:40, _y:-20});
  15.         min.point_to_reach = 0;
  16.         min.speed = 1;
  17.         min.onEnterFrame = function() {
  18.             dist_x = waypoint_x[this.point_to_reach]-this._x;
  19.             dist_y = waypoint_y[this.point_to_reach]-this._y;
  20.             if ((Math.abs(dist_x)+Math.abs(dist_y))<1) {
  21.                 this.point_to_reach++;
  22.             }
  23.             angle = Math.atan2(dist_y, dist_x);
  24.             this._x = this._x+this.speed*Math.cos(angle);
  25.             this._y = this._y+this.speed*Math.sin(angle);
  26.             this._rotation = angle/Math.PI*180-90
  27.         };
  28.     }
  29. };

Line 1: Attaching the path object on the scene

Line 2: Defining an array containing all horizontal coordinates of the waypoints

Line 3: Same thing for the vertical coordinates. Now waypoints are defined, so the minions will walk until (40,140), then until (140,140), then (140,60) and so on.

Line 4: Setting the delay, in frames, between the creation of the xth enemy and the creation of the (x+1)th enemy.

Line 5: A flag variable counting how many frames passed from the creation of the last enemy

Line 6: Variable telling me how many enemies I have placed so far

Line 7: Beginning of the main function, to be executed at every frame

Line 8: Checking if I haven't already placed all 25 monsters I want to place

Line 9: If not, increase the variable counting how many frames passed from the creation of the last monster

Line 11: If the number of frames passed from the creation of the last monster is equal to the number of frames I want to pass after the last monster creation...

Line 12: Increasing the variable counting how many monster I placed. I am about to place a new monster!

Line 13: Setting the variable counting how many frames passed from the creation of the last enemy to zero

Line 14: Attaching the monster movieclip outside the screen, because I want it to enter "from above", and assigning this object to a variable called min (from "minion")

Line 15: Assigning to the minion (I started with "enemy", then "monster", now "minion"... someone should strike for blue triangles rights!) a variable with the number of the next waypoint to reach: zero, because the first element in an array is indexed with zero.

Line 16: Assigning to the minion a variable with its speed. This will become handy in future, when minions will change waling speed during the game

Line 17: Function to be executed at every frame for the minion

Line 18: Calculating the x distance from the current minion position and the next waypoint

Line 19: Same thing for the y distance

Line 20: Checking if the sum of both distances is less than 1 (approx: did the minion reach the waypoint?)

Line 21: If true, then increase the waypoint flag to make the minion move to the next waypoint

Line 23: Determining the angle of the minion according to its direction using trigonometry. If you have troubles with trigonometry, I recommend you to read this tutorial

Line 24: Updating minion's x position according to its direction and speed

Line 25: Same thing for its y position

Line 26: Rotating the minion facing the direction it's walking.

And that's it. The first part is over. You may need to reload the page to see the movie working.

Download the source code and give me feedback

Then move to part 2.

Improve the blog rating this post
Tell me what do you think about this post. I'll write better and better entries.
1 Star2 Stars3 Stars4 Stars5 Stars (12 votes, average: 4.5 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.

102 Responses to “Make a Flash game like Flash Element Tower Defense - Part 1”

  1. Thomas on October 6th, 2007 11:06 pm

    Wicked…

    You never fail to amaze me. All those culy braces :)

  2. Riiich on October 7th, 2007 11:40 am

    Wow, i’ve been looking for a tutorial like this ever since those sort of games have started being made. thanx a lot.

  3. Robby on October 8th, 2007 4:25 am

    AMAZING!!! Can’t wait for part two!!!

  4. Sushant on October 9th, 2007 7:41 am

    Wow,
    I’ve been visiting your blog for quite some time now….I’ve Learnt so much from you. Its been some time since i wanted to make a flash version of my fav game(DotA). N i’ve been trying to find a good path findin AI ideas for it,as flash is not very good at handling complex algorithms.And As usual u again have given me a new idea to play around with. Keep up the good work.

  5. Frederik J on October 9th, 2007 9:14 pm

    Good as usual, emanuele. Keep it up.
    Frederik J

  6. 0wned on October 10th, 2007 9:55 pm

    great tutorial thanks

  7. harryt on October 12th, 2007 3:10 pm

    nice one, i edited the MC and added a few frames to it so that the MC changes whilst it moves

  8. Samuel on October 12th, 2007 9:42 pm

    Hi Emanuele.

    I’m one of those who check your site several times per week, searching for new fantastic tutorials.

    I have one suggestion that would make this game-tutorial REALLY great: Make it OOP. This game is a incredibly suitable game for Object oriented programming, and I think you could add a entirely other dimension to the tutorial if you followed this tip.

    Kindly,
    Samuel

  9. brandon on October 12th, 2007 10:22 pm

    what program did he use because i really wanna make games so could any1 tell me the program name?

  10. Laserman on October 13th, 2007 2:23 pm

    ???
    to hard :D, needs more basic info

  11. oliver_l1 on October 20th, 2007 4:00 pm

    Really great Tutorial!

    Please make a second one.

    Thanks.

  12. jarrod on October 22nd, 2007 12:30 pm

    thanks for the info mate,
    having problems opening the source codes when i try opening it in flash mx 2004 i get unexpected file format error message. am i using the right program?

  13. Joshua threlfall on October 25th, 2007 11:32 pm

    Hey, i set the min.speed to 5 and I had some problems where the minions jumped around in one spot.
    other wise this is a sweet tutorial wheres waiting for part 2!

  14. jabs on November 1st, 2007 3:25 am

    Can’t wait for part 2. I’ve been wanting to create a game like this for awhile and have just started working on it. GLad i stumbled across your blog.

  15. brandon on November 3rd, 2007 9:48 pm

    Im really looking forward to part 2, is it comming out soon? noowhere else offers a tutorial on how to make a tower defense game and i really want to make one..

  16. yogibali on November 8th, 2007 8:58 am

    wow.. that was amazing script…

    Can U make those script more great, by adding the AI script to generate an array of waypoint. :)

    thx alot 4 the tutorial…

  17. zack on November 9th, 2007 3:08 pm

    This is a great tutorial, but I’m having a problem. If my path is not the same as the given one, how do i find out the coordinates of a selected area?

  18. Emanuele Feronato on November 9th, 2007 3:48 pm

    You can find it through mouse coordinates.

    In your main function write

    trace(_root._xmouse+” “+_root._ymouse)

    then move the mouse and copy coords

  19. zack on November 9th, 2007 4:23 pm

    Oh.. Thanks a lot, I’m kind of new to flash.

  20. zack on November 9th, 2007 4:28 pm

    wait.. sorry for double posting but, I’m not sure where to put that code (I’m using Flash Pro 8)?

  21. - on November 12th, 2007 11:53 am

    Hey zack, i did simpler to get coords i think, i just use the window->info panel (or CTRL+I) and while having the focus on the actions window, i move the mouse around the waypoints.

    One little bug is commented by Joshua threlfall, if u make the speed greater than 1 u will get in troubles, this is because….well i dont knowhow to explain i expect that only telling the patch u can understand why doesnt works in that manner
    Just put this in line 20:
    if((Math.abs(dist_x)+Math.abs(dist_y))
    Its a simple matter of gap jumping, u know.

    Oh zack, u must put this code in the first frame of a new layer(created by ur own) called actions by example, selecting the first frame and then open the actions window, paste inside it, be sure of the lower title name is actions:1

    Sorry for my terrible english.
    Congrats by the tut. :D

  22. - on November 12th, 2007 11:57 am

    sorry, the line was incorrect, it was cutted in an amazing manner i dont know, the correct is(line 20):

    if((Math.abs(dist_x)+Math.abs(dist_y))

  23. - on November 12th, 2007 12:01 pm

    urmphh?? sorry again but the post doesnt works well and cut the essential part so i look like a mad….
    The patch is simply reemplace the “if(…

  24. - on November 12th, 2007 12:08 pm

    hahaha this seems a joke, all time cutting my posts i dont know why. The patch. reemplace in line 20 “if(…&lt1)” by “if(…&ltthis.speed)
    Reasons r more or less explained 3 posts before
    Sorry for spaming, the problem was the “&lt” what is an html reserved char and u must write “ampersand + lt”, and first i was disoriented

    I apologize for the inconveniences, sorry for my stupidity xDD

  25. - on November 12th, 2007 12:09 pm

    error again, i dimiss

  26. - on November 12th, 2007 5:08 pm

    Last chance, i swear xD
    Line 20:
    if((Math.abs(dist_x)+Math.abs(dist_y))< this.speed) {

  27. Paul on November 13th, 2007 7:16 pm

    Whenever I put the code in the main frame for the if satement to dry to generate a new enemy….My editor just crashes, so I can never get it to run. Any suggestions?

  28. ben on November 13th, 2007 11:33 pm

    i like cds!

  29. tim on November 16th, 2007 11:42 pm

    Flash MX 2004

  30. Matti on November 19th, 2007 6:14 pm

    He used Macromedia Flash 8 pro

  31. TheInfinity on November 24th, 2007 7:08 am

    I really need help. I just started using this program and found tower defence games very fun. I don’t get how to get to the action script and i don’t get the commands. Do you have to have the items selected before you do those actions? i’mma noob at this ugh… x(

  32. TheInfinity on November 24th, 2007 7:10 am

    srry double posting >.

  33. TheInfinity on November 24th, 2007 7:12 am

    having to post again sorry but, why can’t my macromedia open your sources? keeps saying an error.

  34. zack on November 25th, 2007 5:24 pm

    What program do you use?

  35. mr i dont get it :[ on December 2nd, 2007 1:37 pm

    wow, thats alot of action scripting, i never thought it would be that hard :[ i couldnt even figure out how you got the arow symbol and the background to show up with linkage export path or whatever :S

  36. Luki on December 4th, 2007 12:11 am

    Cool! Will be part 3? With rotating base, and rotating bullets, and attaching new towers? It`s important … :/

    greetings

  37. Stephanie on December 14th, 2007 5:37 pm

    I’m having some issues with the tutorial. For some reason the minion shows up on the screen and looks like its about to follow the path but as soon as the minion hits the background, it just restarts. Please help me

  38. Stephanie on December 14th, 2007 5:38 pm

    I think its something about the delay

  39. Stephanie on December 14th, 2007 5:48 pm

    actually when i put the new_monster = 0; in comment

    if (new_monster == delay) {
    monsters_placed++;
    //new_monster = 0;

    it works but theres only one minion that shows up. Any ideas???

  40. Meepo on December 15th, 2007 8:43 pm

    Augh, im uber-new to flash. I tried to follow this tutorial but when i test movie, all these errors come up. I think its because of the actionscript. What am i supposed to do with it?

  41. ben on December 16th, 2007 6:34 am

    i need help, i cant seem to get the actionscript going. ive done everything it says, but nothin happens when i test it. Can anyone help me?

  42. A on December 16th, 2007 7:06 pm

    This works great except for the fact that when I run it nothing happens at all.

  43. greg on December 22nd, 2007 5:21 am

    when i play i get no syntax error message but the minion just stays in one place

  44. greg on December 22nd, 2007 5:34 am

    nvm, is has to do with linkage

    however, as soon as i got it running, it only spawned one minion,
    what is happening?

  45. pissed off guy on December 30th, 2007 9:06 pm

    look this tut sucks it’s too hard to get. I’ve made every single thing but it doens’t work. Stop making these tuts you’re a horrible explainer

  46. TheInfinity on January 2nd, 2008 9:02 pm

    nothings moving -.- and Flash Pro 8

  47. RJ on January 2nd, 2008 10:54 pm

    You need Flash CS3 to open the .fla’s given by Emanuele, but anyway, you can copy and paste the actionscript. Don’t forget to create the minion and path movieclips and to link them

  48. TheInfinity on January 3rd, 2008 5:49 am

    link them???….MAN i’m an uber noob!

  49. SilverSabre on January 5th, 2008 2:54 pm

    ya im in a simular situation, i made both movie clips and copied the script above but i dont understand linking

  50. TheInfinity on January 5th, 2008 8:05 pm

    ok i got it to work. my “minions” are moving. but when i try to make them faster, they kinda stop at certain points. and stall there for…ever…. -.-

  51. TheInfinity on January 5th, 2008 8:47 pm

    I solved what recently just posted from an earlier post but i have another question. When i test movie in the flash, it’s alright. It works they’re all moving. But when i try the .swf the minions disappear after a certain point on the path (depending on speed) Any ideas?

  52. yikes on January 8th, 2008 1:44 am

    it macromeadia flsh pro 8 im pretty sure :D

  53. With Stephany on January 19th, 2008 1:20 am

    I’m having the same problem as stephany. I just copied the code after making my map and linking the stuff and everything, but whenever a new guy respawns, he moves a bit then just restarts. Any help?

  54. anoyed on January 19th, 2008 7:20 pm

    Yeah, im getting a similar problem… every time I change the speed to 2 or something, they all get stuck when they turn a corner. Any ideas?

  55. stupid on January 23rd, 2008 5:44 am

    Nothing is working at all! Help! The minions don’t even appear on screen!

  56. nicp on January 24th, 2008 5:59 pm

    Somethings wrong…im using Flash MX and everything seems to work fine but my coords are all messed up…i put in my own…but they jump around the screen in the begining and they exaggerate the other waypoints…any help?

  57. Nicolai on January 30th, 2008 8:44 pm

    Eww.. Where do i download that program?

  58. jaasaria on February 4th, 2008 9:38 am

    plss any suggestion on.

    i already created the tutorial but theirs a problem in my work.
    the minion that taking the path is only one. plssss any suggestion.. pllss email me at asaria_ja@yahoo.com
    thxxxxxxxxxxxxxxx
    thxxxxxxxxxxxxxxxxxxxx

  59. Mike on February 20th, 2008 9:12 am

    Hello,

    I am contacting you on behalf of http://www.flashcomponents.net . I’ve read one of your tutorials and I like the way you write. Our site is continuously growing and we recently added a tutorial section. We kindly ask for your approval to allow us to publish your tutorials on our site, mentioning you as the author.
    Of course, we are inviting you to do it yourself, but either way, it would be our pleasure to publish them.

    Kind regards,
    Mike | Flash Components Team

  60. john on February 24th, 2008 2:30 am

    i never made a game in my life befor

  61. jon sploder on March 16th, 2008 8:36 am

    Wow, I never knew flash was pretty much EXACTLY the same as GML :D. Good thing I am fluent, world look out here I come :D. Fantasic tut mate, keep it up!

  62. vub0 on March 18th, 2008 7:43 pm

    i am a beginner and i think u need to explain in more detail b cause i dont understand

  63. z3n on March 30th, 2008 4:31 pm

    this tutorial will do little for a beginner. I took a look at it and tried out some of the code, and I have since had to rewrite and replace almost all of it. It is good for hints how to solve some problems, but the code itself is not well suited for a full featured game.

    Example: the use of clipExample.onEnterFrame = function is a bad tree to bark up. The variables that are loaded local to those clips do not update, such as array lengths. So, when that clip loops through arrays (each frame) it is using the length property it got when the clip was loaded.

  64. niki on April 2nd, 2008 2:37 pm

    what is the nme of the program u use?

  65. Chris on April 9th, 2008 12:54 am

    What IS the program you used???

  66. Chris on April 9th, 2008 12:54 am

    What IS the program you used???

  67. Chris on April 9th, 2008 12:57 am

    PLEEEEEEEEEEEEEASE tell me where to download it!!!!!!!!!

  68. Alan on April 13th, 2008 10:44 pm

    Please, start from the beginning. What program do you use. Send me every step, please

  69. D on April 14th, 2008 7:04 pm

    It’s flash (CS3 being the latest version, this is actionscript 2 though).. You can download a trial at adobe.com. I would’nt reccommend starting with this tutorial though, it’s not for absolute beginners.

    Linkage:
    Right-click your symbol and choose linkage. Then check “export for actionscript”.

    The “jumping” minion problem is caused by where you’ve set the registry point. It has to be exactly in the middle of you little dude.

  70. stojan on April 23rd, 2008 11:58 pm

    OMG its only me or i dont anderstent on what should i sett the codes on the path or minions and when i copy the codes to the minion that i fink is the right i got 7 errors HELP pleas

  71. Zackorith on April 26th, 2008 9:47 pm

    Well personally i though this tutorial was easy, and i understood it all

    i has a loader, money, kills

    but i cant seam to make it so when minion gets to the end you lose a life, and then plays next level…
    so how do i makes lives go down o.0??

    Zackorith2@hotmail.co.uk

    i understood everything else (Y)

  72. flashmxguy on April 27th, 2008 8:21 pm

    i gotta say your tutorial for this sucks.what do i do after i draw the minion cause i really dont know you say stuff but u dont say do that and that .i dont know how to see the minion i just press timeline sfter i draw him and i dont see it on the right so where is it?arrg

  73. ikiki on April 30th, 2008 1:35 am

    i don’t get it to u need some softwar? if so wat one? is it free or not?

  74. Mic on May 6th, 2008 8:24 am

    Thanks for the tutorial. I have a personal project that this is perfect for!

  75. HELP on May 8th, 2008 3:57 am

    wtf? I copied ur actionscript, made 2 movie clips called minion and path
    but nothin works

  76. Van Halen on May 10th, 2008 3:33 am

    where is the main function thing so i can get to see the coordinates? could you just post a copyable picture of the path so i can copy and paste it?

  77. superman on May 18th, 2008 10:37 am

    YES!!!

    i just L- O- V- E when my search works and the actionscript works and my own MODS work… XD

  78. Pvpdave on May 26th, 2008 6:38 pm

    this is so awsome i cant wait to show my class :D ^^

  79. Linder on June 2nd, 2008 11:05 pm

    Sorry for being so dumb, but what program is used? I really love TD’s and i got alot of spare time over. So i think I could try to make one, maybe it will be good :).
    I would like to have an answer as soon as possible.

    Linder

  80. Matthew Baumann on June 11th, 2008 1:33 am

    Hi. I’m using Macromedia Flash MX Professional 2004. I was wondering if anyone could help me get started with this. I can’t get this to work. Any help would be very much appreciated. You can reach me at mattbaumann@hotmail.com

  81. kid programmer on June 27th, 2008 3:16 am

    OH the program is called flash. i have the flash 5. You can buy the new version 8 for like 100 dollars lol. Or you could get an old one. XD

  82. kid programmer on June 27th, 2008 3:58 am

    this doesn’t work on flash five :(

  83. daniel on June 28th, 2008 1:06 pm

    Hello,
    Just to thank you on a great tutorial it helped alot.

    Dan

  84. kid programmer on June 28th, 2008 9:17 pm

    i was wondering could you translate this to actionscript 1.0? Flash 5 doesn’t use actionscript 2.0. Sorry for all the comments.

  85. MaTt on July 11th, 2008 3:52 pm

    my minions do nothing but stnd still email me if you can fix this stickdude45@hotmail.com

  86. ludde on July 18th, 2008 6:14 pm

    this is cool but i need win rar

  87. Mephistopheles on July 18th, 2008 7:56 pm

    This is all great, but what do we do it on?

    Is there like a program you used?

  88. Shedokan on August 19th, 2008 9:11 pm

    How can I make it with a lot of towers?

  89. ErzeN on August 26th, 2008 9:16 am

    wooowww that is soo cool :D

  90. Frogger on August 29th, 2008 9:34 pm

    Brandon, this is a little late, but if you don’t know what Flash is, then it will really be hard to make games, let alone use it. So anyway, the program he used is Macromedia Flash.

  91. Alexius Lengele on September 2nd, 2008 6:02 pm

    u guys are noobs

Leave a Reply




Trackbacks

  1. Games » Make a Flash game like Flash Element Tower Defense - Part 1 on October 6th, 2007 11:49 pm

    [...] unknown wrote an interesting post today onHere’s a quick excerptOne of the most popular Flash games around the web during last months was a game (with some variants) called Flash Element TD, “a Macromedia Flash based Warcraft TD game inspired by Element TD for WarcraftIII”, as says its author, … [...]

  2. GadgetGadget.info - Gadgets on the web » Make a Flash game like Flash Element Tower Defense - Part 1 on October 6th, 2007 11:58 pm

    [...] biskero wrote an interesting post today!.Here’s a quick excerptOne of the most popular Flash games around the web during last months was a game (with some variants) called Flash Element TD, “a Macromedia Flash based Warcraft TD game inspired by Element TD for WarcraftIII”, as says its author, … [...]

  3. www.gamesandgames.info » Make a Flash game like Flash Element Tower Defense - Part 1 on October 7th, 2007 8:41 pm

    [...] Emanuele Feronato wrote a fantastic post today on “Make a Flash game like Flash Element Tower Defense - Part 1″Here’s ONLY a quick extractOne of the most popular Flash games around the web during last months was a game (with some variants) called Flash Element TD, “a Macromedia Flash based Warcraft TD game inspired by Element TD for WarcraftIII”, as says its author, … [...]

  4. Make a Flash game like Flash Element Tower Defense - Part 2 : Emanuele Feronato - italian geek and PROgrammer on November 6th, 2007 9:35 pm

    [...] Welcome to the 2nd part of this tutorial. I recomment you to check part 1. [...]

  5. Hoi! Koffie? » Tower “Addiction” Defense on November 24th, 2007 9:06 pm

    [...] Make a Flash Game Like Flash Element Tower Defense - Part 1 [...]

  6. Oman3D.com » Blog Archive » Best of the Internet - November 2007 on November 30th, 2007 8:30 pm

    [...] Flash Game Tutorial - Element Tower Defense [...]

  7. A Gem of Flash Game Tutorials | Newbie Game Programmers on December 17th, 2007 4:22 pm

    [...] a couple of hints on how to make these Tower Defense type games. This is when I came across a post Make a Flash game like Flash Element Tower Defense, but instead of finding 1 great Gem, I found an entire website worth of [...]

  8. Desenvolvimento de games « O Coice da Girafa on January 25th, 2008 3:36 am

    [...] Desenvolvimento de games Emanuele Feronato - Parte 1 [...]

  9. digitalt skapande » Tower Defence on April 21st, 2008 2:49 pm
  10. Adobe Flash CS3- AS3 and AS2 game tutorials roundup | Lemlinh.com on August 19th, 2008 6:35 pm

    [...] Part 1 [...]

  11. Adobe Flash Tutorials Part 2 on September 9th, 2008 4:15 am

    [...] Tower Defense the concept is : there is a road, and some minions walking that road. You have to kill all minions before they reach the end of the road. You can build several facilities to kill minions and earn money to upgrade weapons as the minions get stronger and faster. [...]