Now that we have all of the visible objects on the screen, we can move to the collision detection. Collision detection is actually the easy part. We have already written code that looks suspiciously like collision detection in the createWall
and
createMouse
methods. The checks we perform are nearly the same, except we are concerned only with collisions involving the head of the snake, since it is the only part of the snake that can collide with another surface. Let's look at the
checkForCollisions
method in two sections. The first section contains the checking for game ending crashes.
Filename: SNPlayfieldLayer.m
(checkForCollisions
, part 1)
-(void) checkForCollisions { // Get the head SNSnakeSegment *head = [[snake snakebody] objectAtIndex:0]; // Check for collisions with the snake's body for (SNSnakeSegment *bodySeg in [snake snakebody]) { if (CGRectIntersectsRect([head boundingBox], ...