Game Development Reference
In-Depth Information
move or shoots the wrong bullets, you'll get a crash that waves a big red flag saying,
“Hey, you forgot to update me!”
It's also perfectly fine to run code before the assignment to self , as long as you don't
forget to call a [super init. . .] method eventually. Otherwise, the superclass
won't be properly initialized, and that can lead to strange bugs and crashes.
The component classes created and added to Enemy contain exchangeable code used
to extend the behavior and looks of the enemies. I'll get to components soon; for the
moment, just know that the StandardMoveComponent class allows the enemy to
move, whereas the StandardShootComponent allows it to—you guessed
it—shoot. The necessary import statements are at the beginning of Listing 8-8 .
Focus your attention now on the initSpawnFrequency method in the Enemy
class. The relevant code is shown in Listing 8-9 .
Listing 8-9. Controlling the Spawning of Enemies
staticNSMutableArray* spawnFrequency = nil;
-(void) initSpawnFrequency
{
// initialize how frequently the enemies will spawn
if (spawnFrequency == nil)
{
spawnFrequency = [NSMutableArray arrayWithCapacity:EnemyType_MAX];
[spawnFrequency insertObject:[NSNumber numberWithInt:80]
atIndex:EnemyTypeUFO];
[spawnFrequency insertObject:[NSNumber numberWithInt:260]
atIndex:EnemyTypeCruiser];
[spawnFrequency insertObject:[NSNumber numberWithInt:1500]
atIndex:EnemyTypeBoss];
// spawn one enemy immediately
[self spawn];
}
}
 
 
Search Nedrilad ::




Custom Search