Much of the remaining functionality is in the
update
method. There are three distinct sections of the loop, and we will look at them in turn. The first section handles the movement updates.
Filename: SNPlayfieldLayer.m
(update
, part 1)
-(void)update:(ccTime)dt { stepTime += dt; if (stepTime > snake.snakeSpeed) { stepTime = 0; [snake move]; [self checkForCollisions]; }
We use a standard delta time counter to keep adding to the
stepTime
variable. On each loop, we check if the stepTime
is greater than the snakeSpeed
variable. If it is, then we need to move the snake. As discussed in the previous sections of the chapter, all we have to do is call the snake's move
method. The only time we need to check for collisions is after the snake's move
method is called, so we call that method next.