Game Development Reference
NSAssert(enemyType < EnemyType_MAX, @"invalid enemy type");
NSNumber* number = [spawnFrequency objectAtIndex:enemyType];
First the method asserts that the enemyType number is actually within the defined
range. Then the NSNumber object for that enemy type is obtained and returned as in-
The modulo operator % returns the remainder left after the division of the two operands
updateCount and spawnFrequency . This means an enemy is spawned only
when updateCount can be evenly divided by spawnFrequency , resulting in a re-
mainder of 0.
The spawnEnemyOfType method then gets the NSMutableArray from the en-
emies NSMutableArray , which contains the list of enemiesOfType , another
NSMutableArray . You can now iterate over only the desired enemy types, rather
than having to go through all sprites added to the CCSpriteBatchNode . As soon as
one enemy is found that isn't visible, its spawn method is called. If all enemies of that
type are visible, the maximum number of enemies is currently onscreen, and no further
enemies are spawned, effectively limiting the number of enemies of a type on the
screen at any time.
Finally, to have enemies appear on screen, open the GameLayer.m file and extend
the init method with the code that initializes the EnemyCache . Add this code just
above the BulletCache initialization, and don't forget to import the
EnemyCache.h header file at the top of GameLayer.m :
. . .
if ((self = [super init]))
. . .