Welcome to another installment of my tower defense series – Part 9. I know you’ve been asking for more and I’ve done my best to get creative. I’ve already built out the next few Parts the only thing I need to do now is explain it all, but for now without any more delay – This lesson we will be covering the following subjects:
- Multiple Waves of creeps at the same time.
- Creation of a time line for creep releasing.
- Start tracking a users score in a full game.
Before we begin, if you haven’t read How to build a tower defense flash game – Part 8 – Poison Tower and Basic Startup! you may become overwhelmed at the scope and content and should consider going back and read the earlier articles which may answer some questions if you came here first.
As always the source code for this tutorial can be downloaded by clicking here
So, it’s looking pretty good so far, we obviously have come a long way since setting way points and following basic paths. Some of the things we are setting up now are to add a little excitement and competition to the game and what better way then add the concept of scoring to the mix. Why does a game need scoring? A game needs scoring to keep it fresh, otherwise the game is just about getting through it once. Sure you might once in a while play a game twice, because a specific section was especially challenging, but for the most part without a method to make competition a part of the game – even on a great game – the game life will be shorter on games without scores.
SIDE NOTE: Community Features
When you add a personal high score the game is giving you a personal benchmark to achieve. The game could be terrible, but it isn’t about it being bad as much as it is now a
challenge to the player himself (”I know I can beat that” or “Just one more time and I can do it”). The personal benchmark takes on a new meaning when introduced into a half decent game, dare I say, a game the player actually enjoys. Another level of achievement is had when you add a leader board to the mix – now you’ve taken the concept of a personal high score and introduced the world. Now it isn’t as much as achieving your own personal goal, but get into the mix of beating everyone around you. There are different types of leader boards, but I believe the “daily”, “weekly”, “monthly” and “all time” are pretty good starts. If you can add “Easy”, “Medium” and “Hard” settings to the mix all the better – Have leader boards for each other them and I guarantee that people will play your games longer than they would normally. Just remember, these “community” features will add life to your games, but it it only helps magnifying glass for the game – if the game is terrible, it might make it just playable – HOWEVER – if the game is great, it will make it immortal.
END SIDE NOTE
So for the most part this tutorial will go over adding the scoring element and some features to distiguish the good players from the great. Ok, so to that end we obviously need to do a little setup. For one thing we are going to need to move code around a little and make some new variables. In fact most of the code change relating to score will all take place in the actionscript layer in the main timeline (We try to group code when we can). So lets get right to it.
We’ve created a new varaible, surprise surprise, it’s “_root.score” which will contain the players current score as they progress through the game.
The next item is more interesting, it is a new function called “executeCallback” – I’m really unoriginal with my names, but at least you can be fairly certain how this function will be called. We will call this function everytime we want to start a new level or when it’s not progressing to the next level it’s increasing the “_root.count” by 1. In fact you will soon see that this will be called every second when we put in a timeline! This function will serve as the progress bar counting down the time until the next wave release.
This is the meat and potatoes of the score function. You will need to put “updateScore()” into the following function calls to make sure they give score updates at the appropriate time:
- When the function newLevel() is called.
- When a creep is killed.
- When a tower is upgraded.
What the code does is first put the current gold amount in a temporary variable “a” and then loop through all the towers and check to see which are still active and add the cost of that tower to variable as well. At the end we take that temporary variable and store its value back into _root.score. As you can imagine if you have a lot of towers or if you save a lot of gold, you can score a bunch of points, however, only a good plan will determine whether saving money or spending it pays out in the end.
This is the last real update to the actionscript code in the main timeline. This might be lost in the code change, so I felt it was important to point out. This function originally only updated the “gold” amount when the player was killing creeps. Now that we have a score though, it has taken on the expanded role of updating the score as well. I haven’t changed the functions that called it since the last tutorial, because any time we are increasing or decreasing gold, we are also updating the score as well.
Just to keep you informed – This does mean that you will need to create a new text field in the movie clip “InfoBar” with the instance name of “ScoreAmount”, but that shouldn’t be too hard for a tower defense master like yourself.
Now to the Timeline
So what’s a “wave” time line you ask. Well, it’s easy, instead of just having a level end when the towers are all destroyed or a timer has been reached, we give the player the control over when the next wave takes place. We also give an added incentive that you score more points if we release the wave before the next wave is scheduled to be released. Again, you can see how we re tapping into the users desire to get bigger scores (It doesn’t hurt that the average end score can be like 100,000 points – my best was 534,680 points).
So to do this we need to make a “timeline” you can see the code below:
Basically we just do a simple mask for a large line of colorful rectangles that *eventually* will be different types of creeps. The distance of each rectangle is such, that when the timer starts the full rectangle will move off the screen as the next wave is released. The action script code is setup to align “programmatically” so the first rectangle is in the correct position when the game loads for the first time.
This is where all the fun happens.
When this code is first run, we create a timer interval that runs every 1 second and calls our function above “executeCallback”. The function does nothing while the game has not yet been started. When the game is started, the player can click on the timeline and it will automatically set the “nextLevel” in motion – giving an interest on money saved, resetting the “Count” variable and sending the next wave. This has the awesome benefit that we can get multiple creep waves on the same screen for a crazy experience.
You can now get a whole new experience then before with this type of functionality. Don’t mind my non descriptive art, you could lable the timeline’s rectangles with whatever you want. A red rectangle could be a flying creep, a blue a strong creep, green a groups of closely knit creeps, etc. Try it out and post some feedback with your scores… I’d love to see if you can beat my score.
That’s it for now – How about checking out the next part while were on a roll -
You can click here to go back to the introduction