Game Development Reference
In-Depth Information
+(int) getSpawnFrequencyForEnemyType:(EnemyTypes)enemyType
NSAssert(enemyType < EnemyType_MAX, @"invalid enemy type");
NSNumber* number = [spawnFrequency objectAtIndex:enemyType];
return number.intValue;
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-
tValue .
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 :
#import "EnemyCache.h
. . .
-(id) init
if ((self = [super init]))
. . .
Search Nedrilad ::

Custom Search