Ok, so based on the comments I realized that not having a “win” or “lose” condition at this point was not working. Well, fear not my friends, I have fixed the situation and now you can lose or win depending on how you play the game now. Yay!
Having a win/lose condition at this point is no good if you don’t know how anything else works. Therefore, If you haven’t read How to build a tower defense flash game – Part 9 – Scores and Wave Timeline! you should go back and read it before you continue.
As always the source code for this tutorial can be downloaded by clicking here
Man, I have to admit, that I am pretty excited about this one, because this is the first time I can say that we have a complete and working game. You can win and lose and it’s all up to the player to prove his worth.
To accomplish all we needed to have a win/lose ending, we needed to introduce a few new variables:
- _root.health = 20;
We use this variable to keep track of the players current health.
- _root.hasGameFinished = false;
We use this variable when the game is over and use it to add to “tower” explosions when you win or lose – We also
- _root.currentNumberOfCreeps = 0;
We need a new way to keep track of the current creeps on the map at any time – unfortunately, _root.creepCount doesn’t fit for us since it is used to not only keep track of the total creeps that have ever existed, but we use it for create the movieclips and therefore we can use it.
![]() |
We’ve added new code to leak() to now account for the removal of health. We now remove one point of health for each creep that the player lets through. Once the health value falls to “0″ we see if the _root.hasGameFinished is still false and we tell the user that he lost then we set _root.hasGameFinished to true so it doesn’t get run again. Pretty neat huh!
![]() |
We add an additional line to the UpdateInfoBar function to account for the new “health” field. We didn’t need to add any additional calls to the function though since we have it called enough – Whenever the creeps make it through the defense this will be updated with the new correct health value.
![]() |
For a winning condition to to take place we need to keep track of how many creeps are around and what level we are on. This is the function we use to keep track of that fact. When a creep is created we increase _root.currentCreepCount by one and when a creep is killed we remove one from it as well. Once we hit level 35 (that is currently the last level) we reallly start to pay attention to that value and when it is zero we know that the game is over.
Whether the game is won or lost by the player, we do the same animation – we blow up all the towers. It’s a cool effect and all we need to do to make it happen is to loop through all the towers and call “Explosion” for the x/y coordinates of the given tower. Originally it was only done for lose condition, but I liked it so much I remove the check and made it happen for the win condition as well.
Well, it’s a quick one, but we have a complete game – Don’t you fret though, while it is a complete game we still have more work to do… Until next time… You can click here to go back to the introduction




please there is a problem, i just made a “gameover frame” that the game goes to when the player wins or loses or when the player hits the “end game” button but the problem is i can’t seem to make all creeps on stage to leave the screen when the player gets to the “gameover frame”. Please can you make a code for that?
please how can i also make the gold coins, lives and scores reset to zero when the player wants to play again? I am working on my game, thanks to you
Same problem. I can’t figure out how to reset the game
Hey Scott, the gold coins will automatically reset to the original number, i just tried it. I just found a way to make the creep and tower leave the stage. Just copy and paste this in your “end” button or whatever button makes the player leave the frame.
on (press) {
_root.remove = true;
gotoAndStop(1); whatever frame you want the player to goto written in the ()
}
paste this in the main creep with the health bar (creep1)
onClipEvent (load)
{
}
onClipEvent (enterFrame)
{
if(_root.remove == true){
this.removeMovieClip();
}
}
Paste this in your main timeline above everything else
var remove = false;
I will try the rest later like trying to remove just one tower from screen after it has been placed, something like a “sell” button.
thanks Kris. thius helped a lot.
that sell idea would be great though i have no idea how to do it.
hope you figure it out, if you do could you please tell me how, that would be great.
Hey everyone, I was wondering how you can insert more towers into the coding of the game to more variety to the choices of towers, i’ve tried to make more towers available to buy but I can’t make it work. If anyone can help me with this please let me know how, thank you.
hey Jack, it depends on what you want the towers to do. if you want just the regular type of towers (damage, splash, poison, slow) all you have to do copy and paste the code of any already designed tower into your new tower
onClipEvent (load)
{
title = “Cannon Tower [Cost: $15]“;
desc = “Medium attack speed with high damage and a high range”;
type = “Ground”;
something like that…
give the tower instance name in the properties menu of your flash movieclip like (tower_cannon but change the cannon to something else because there is already one tower called that, call it tower_cool). Then copy that same tower_cool again and paste it on stage where the other tower were also placed on stage like a duplicate. copy the code from any tower on stage..
on (press)
{
if (_root.gold >= 20)
{
_root.marker.active = 1;
_root.marker.tower = “cannon”;
something like that…
And paste it in your tower but remember to change the “cannon” or whatever it is written after the _root.marker.tower = “…..”; to the name of your new tower which was called “cool”. thatz it very simple. Just change the
upgrades = [["Damage: 40\rRange: 100\rSplash:No\rSlow:No", 20, 40, 100, 5, 0, 0],
["Damage: 75\rRange: 100\rSplash:No\rSlow:No", 30, 75, 100, 5, 0, 0],
to whatever you want and your tower will have different functions of your choice
I was wondering if there was a way you could make the waves of enemies spawn from more then one Way point’s on the top of the screen and go towards the bottom of the screen and stop and attack. I have a base at the bottom of my screen and they go down and stop but the only spawn from one even though I coppied the spawn function and changed what way point they spawn from and what one they look for after they get to it. If anyone has done something like this could you please let me know how you did it? Thanks
Also I was wondering why the upgrades for the towers dont change. I changed the code on the tower placement actions page and when I test it, it doesn’t change in the game.
i also want to make multiple starting waypoints but i haven’t realy figured it completely yet.
what do you mean change the upgrade for the towers? do you want to change the movieclips when the a particular tower is being upgraded or you just want to change what is written on the UpgradeTowerInfo?
My tower defense game is almost complete now, i just need to give it some more polish. You should think of adding your own script to your tower defense game to make it more innovative rather then the regular tower defenses out there.
I mean when I change what the text says for the upgrades it still just goes to what it was before I changed it. Like if the next upgrade was 80 dmg and i changed it to 120 instead it would still just go to 80.
if you want to change the text for the upgrade for towers, just click on the tower and change the actionscript that says
upgrades = [["Damage: 400\rRange: 80\rSplash:No\rSlow:No", 200, 400, 80, 5, 0, 0],
Starting from your right of your screen after the rSlow:No” is the part that makes the changes happen, the first part that says 200 is the amount of gold to buy that upgrade, the part that goes 400 is the damage (that is what you change to what you want to actually happen in damage), then range and so on. to change the text that shows on the tower upgrade, you go to the left part of your screen, the part that starts with “Damage: 400\rRange: 80…. change it to what you wrote at the right part of your screen. like in my game, i made the damage to 400 in both sides.
Hope i explained it well
[...] How to build a tower defense flash game – Part 10 – Win or Lose! [...]
Just wanted to say thank you for posting, and keeping, the tutorial. Was VERY helpful as both a general introduction to Flash games and the mechanics behind tower defense games. Two friends and I rigged up our own game using the information you provided, and we would have never been able to build one as robust and full of features- as well as adding more things on our own- had it not been for the tut. On behalf of our humble team, thank you.
You should add a part to teach us how to sell towers and show creeps’ info (when clicked). Thanks for the awesome tutorials!
I learned a lot from this tutorial. I have taken this very far, and almost all of it has your code, but it is very different. I added a sell button and it was very hard, but I think you guys should make a tutorial on it because everybody it begging for it, I am terrible at making tutorials but beginners should learn this. Basically you use the unloadMovie and _root.gold += _root.totaloverallvalue and the value is 70% of the cost and all upgrades, but I can’t explain it all too well. I thank you, and i have referred some friends here.
Thanks for this tutorial, it really was enlightening.
I think this tutorial is so helpful. Here are some things I’d like some help with, please.
-Pause button
-Fast Forward button [if possible]
-Different creeps for variety?
-Is it possible to have different graphics for bugs to represent higher upgrade levels instead of clicking on them in the middle of a round?
Really love this tutorial. It certainly taught me some great things!
Wow Alex, that little snippet was very helpful! I’m having trouble implementing it so I really hope someone can do a full tutorial on it!
Another thing you might want to go over is VISUAL upgrades. (When you upgrade a tower, it gets cooler/bigger/different color.
All in all an awesome tutorial, though.
Hello there, I discovered your blog by the use of Google while looking for a comparable topic, your site got here up, it appears good. I’ve bookmarked it in my google bookmarks.
Ok i started this tutorial developed on it and now i am bringing a number 2 of my tower defence game so because i like givving heres the sell btn im using sells the tower for half its original price.
dont copy straight away read it and look where the functions go.
function towerInfo(tower)
{
_root.selectedTower = tower;
_root.ranger._x = tower._x;
_root.ranger._y = tower._y;
_root.ranger._width = tower.range * 2;
_root.ranger._height = tower.range * 2;
_root.UpgradeTowerInfo._visible = true;
if (tower.upgrades.length >= tower.level)
{
_root.UpgradeTowerInfo.UpgradeInfoText.text = tower.upgrades[tower.level - 1][0];
_root.UpgradeTowerInfo.TowerInfoText.text = “Damage: ” + tower.damage +
“\rRange: ” + tower.range +
“\rSplash: ” + (tower.splash?”Yes”:”No”) +
“\rSlow: ” + (tower.slow?”Yes”:”No”) +
“\rPoison: ” + (tower.poison?”Yes”:”No”);
_root.UpgradeTowerInfo.UpgradeText.gotoAndStop(1);
_root.UpgradeTowerInfo.UpgradeButton.UpgradeButtonText.text =
“Upgrade [$" + tower.upgrades[tower.level - 1][1] + “]”;
_root.UpgradeTowerInfo.UpgradeButton._visible = true;
_root.UpgradeTowerInfo.sellbtn._visible = true;//put this line here
}
else
{
_root.UpgradeTowerInfo.UpgradeText.gotoAndStop(2);
_root.UpgradeTowerInfo.f.text = “”;
_root.UpgradeTowerInfo.UpgradeButton._visible = false;
_root.UpgradeTowerInfo.sellbtn._visible = false;//and this here
_root.UpgradeTowerInfo.h.text = “”;
} // end else if
}
put the lines in where it shows you in the towerInfo Function
put this at the bottom of your script.
function sell(){
d = _root.selectedTower;
_root.gold += d.cost;
d.removeMovieClip();
_root.UpgradeTowerInfo.f.text = “”;
_root.UpgradeTowerInfo.UpgradeButton._visible = false;
_root.UpgradeTowerInfo.h.text = “”;
_root.UpgradeTowerInfo.sellbtn._visible = false;
_root.ranger.gotoAndStop(4);
updateInfoBar();
}
make a cell btn in the tower info panel and call it “sellbtn”
put this script in the sellbtn
on(release){
_root.sell();
}
Now last but not least go into your towers scripts and change the cost variable to how much the tower will sell for dont make it more then what you buy the tower for are people could earn money.
this will not effect how much you buy your tower for
Ah man, this tutorial is ok, but not great. First off, the fact that you scripted objects as well as the frame really is annoying. It is much better to simply use multiple layers and put code into each one, instead of this method. Yes it works, but its still is annoying. Further more, why did you use entities, and gave them instance names, in order to create waypoints. Why not just use an array and plot points in it? Ah well, good tutorial for beginners, but bad for us intermediate programmers.
Greetings! I really like the tutorial. I’ve made it all the way here and everything’s working quite well now. I’ve made several different maps and even some new baddies. Now, what I’m trying to do is have enemies spawn from more than just one location. Does anyone have any advice on how I could implement something like this here?