Game Development Reference
In-Depth Information
makes this type of collision detection a radial check . For this type of game, it's suffi-
cient. Add the call to [self checkForCollision] to the end of the -(void)
update:(ccTime)delta method, along with the resetGame method, which re-
sets the spiders.
Listing 4-13 . A Simple Range-Check or Radial Collision-Check Suffices
-(void) checkForCollision
// Assumption: both player and spider images are squares.
float playerImageSize = player.texture.contentSize.width;
CCSprite* spider = [spiders lastObject];
float spiderImageSize = spider.texture.contentSize.width;
float playerCollisionRadius = playerImageSize * 0.4f;
float spiderCollisionRadius = spiderImageSize * 0.4f;
// This collision distance will roughly equal the image shapes.
float maxCollisionDistance = playerCollisionRadius + spiderCollisionRadius;
int numSpiders = spiders.count;
for (int i = 0; i < numSpiders; i++)
spider = [spiders objectAtIndex:i];
if (spider.numberOfRunningActions == 0)
// This spider isn't even moving so we can skip checking it.
// Get the distance between player and spider.
float actualDistance =
ccpDistance(player.position, spider.position);
// Are the two objects closer than allowed?
if (actualDistance <
// Game Over (just restart the game for now)
[self resetGame];
Search Nedrilad ::

Custom Search