Game Development Reference
In-Depth Information
You spawn an enemy with the spawn method:
-(void) spawn
// Select a spawn location just outside the right side of the screen
CGSize screenSize = [CCDirector sharedDirector].winSize;
CGSize spriteSize = self.contentSize;
float xPos = screenSize.width + spriteSize.width * 0.5f;
float yPos = CCRANDOM_0_1() * (screenSize.height - spriteSize.height) + ←
spriteSize.height * 0.5f;
self.position = CGPointMake(xPos, yPos);
// reset health
hitPoints = initialHitPoints;
// Finally set yourself to be visible, this also f lag the enemy as "in use"
self.visible = YES;
Because an EnemyCache is used to create all instances of enemies up front, the whole
spawning process is limited to choosing a random y position just outside the right-hand
side of the screen and then setting the Enemy sprite to be visible. The visible status
is used elsewhere in the project, specifically by component classes, to determine
whether the Enemy is currently in use. If it's not visible, it can be spawned to make it
visible, but it should run its game logic code only while it's visible.
The EnemyCache Class
I just mentioned the EnemyCache class, which is also new in the ShootEmUp02 pro-
ject. By its name, it should remind you of the BulletCache class, which also holds a
number of pre-initialized objects for fast and easy reuse. This avoids creating and re-
leasing objects during game play that can be a source of minor performance hiccups.
Especially for action games, those small glitches can have a devastating effect on the
player's experience. With that said, look at the unspectacular header file of the
EnemyCache in Listing 8-10 .
Listing 8-10. The @interface of the EnemyCache Class
Search Nedrilad ::

Custom Search