How To Build a Tower Defense Game for the iPhone – Part 5a – Multiple Waves
[Editor's Note: One of our readers spoke up and decided to help us finish off this series of tutorials. Many thanks to Aiden Fry for stepping up to the plate!]
Welcome to the latest edition of the Tower Defense Tutorial. This is just the start of the long awaited renewal of the Tower Defense Tutorials. I (like many of you I am sure), have been following this particular tutorial series with interest, and was slightly downcast when I realized that part 4 was the last to be released. So hear me now, I vow to take up the challenge and by the end of these tutorials we will have a fully working game!
Yours, Aiden.
[Editor's Note: We are also very happy that Aiden has taken up the challenge as well!]
The Tower Defense Tutorial Part 5a – Multiple Waves!
A Quick Look Back:
I will assume that all of you have been following the tutorial series found here – part 1, part 2, part 3, part 4 – and are mostly OK with what has gone before, however it is good just to mention a few key things about what has already taken place.
- We have some creeps that go around a set path.
- We can place towers around the path.
- Towers fire at the nearest Creep.
- Creeps have Hp, and when HP is 0 (due to tower projectiles) the creeps “die” and are removed from the game.
- At the moment there is only one “Wave” of Creeps.
Aims of this Tutorial:
- Creating Multiple waves of Creeps.
- Defining how many Creeps of each type (red/green) per wave.
Source code after the break…
Here is the source code: Tower Defense Part 5a. Once again, we’re using the Cocos2d framework which you can download here!
To create multiple waves, first we must define multiple waves, this is done in addWaves in TutorialScene.m, originally it should look like this.
-(void)addWaves {
DataModel *m = [DataModel getModel];
Wave *wave = nil;
wave = [[Wave alloc] initWithCreep:[FastRedCreep creep] SpawnRate: 0.3 TotalCreeps:50];
[m._waves addObject:wave];
wave = nil;
wave = [[Wave alloc] initWithCreep:[StrongGreenCreep creep] SpawnRate:1.0 TotalCreeps:5];
[m._waves addObject:wave];
wave = nil;!
}
Hold up: The above means that there are two waves defined, the first wave has a total of 50 creeps, and spawns a knew one at 0.3 creeps per second. The initWithCreep[fastred] or [stronggreem] actually means nil to zilch at the moment (as we are defining which creep type to spawn at random in addTarget) therefore I will be “init-ing” with fast red creep from now on to avoid confusion.
After we define our wave, we add it to an array for storage/call back later and initialize our wave variable to nil. Adding our own waves: Yep you guessed it, to define our own waves all we need to do is to add more lines to the addWaves function, and just adjust the spawn rate and totalcreeps (if you so wish). I chose to create 5 waves, that will increase in difficulty as we progress.
-(void)addWaves {
DataModel *m = [DataModel getModel];
Wave *wave = nil;
wave = [[Wave alloc] initWithCreep:[FastRedCreep creep] SpawnRate: 1.0 TotalCreeps:10];
[m._waves addObject:wave];
wave = nil;
wave = [[Wave alloc] initWithCreep:[FastRedCreep creep] SpawnRate: 0.7 TotalCreeps:20];
[m._waves addObject:wave];
wave = nil;!
Page 1 of 3 | Next page