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! [...]