Game Development Reference
In-Depth Information
Listing 8-16. Performing the Collision Checks in the EnemyCache Class
#import "BulletCache.h"
#import "GameLayer.h"
. . .
-(void) update:(ccTime)delta
{
. . .
[self checkForBulletCollisions];
}
-(void) checkForBulletCollisions
{
for (Enemy* enemy in batch.children)
{
if (enemy.visible)
{
BulletCache* bulletCache = [GameLayer sharedGameLayer].bulletCache;
CGRect bbox = enemy.boundingBox;
if ([bulletCache isPlayerBulletCollidingWithRect:bbox])
{
// This enemy got hit . . .
[enemy gotHit];
}
}
}
}
Here again it's convenient to be able to iterate over all the enemy entities in the game,
skipping those that are currently not visible. Using each enemy's boundingBox to
check with the BulletCache isPlayerBulletCollidingWithRect meth-
od, you can quickly find whether an enemy got hit by a player bullet. If so, the Enemy
method gotHit is called, which simply sets the enemy to be invisible for now if there
are no more remaining hitpoints:
 
Search Nedrilad ::




Custom Search