Game Development Reference
In-Depth Information
The preceding method is provided with the contact that has two properties in it,
bodyA
and
bodyB
. But these bodies are somewhat randomly selected and we need
to check their type before we do anything. Here we check if we picked up the
power-up on which we set the
shielded
property on our player to
YES
, and if we
touch the enemy, we will call the
takeDamage
method on the player.
We will need to modify
ERGPlayer.m
by first removing
self.shielded = YES
and
self.shielded = NO
from where we put them before. They will be handled by
collisions from now on. After this, add a new method to handle taking damages:
- (void) takeDamage
{
if (self.shielded) {
self.shielded = NO;
} else {
self.hidden = YES;
[[NSNotificationCenter defaultCenter] postNotificationName:@"p
layerDied" object:nil];
}
}
Since we want the player node to be as decoupled from our scene as possible, we won't
call some method on the scene directly, but we will broadcast notifications to everyone
who might be interested in it. In our game, we will handle it by presenting a new
Game Over scene to the player.
Scene transitions
Only one scene may be present on-screen at any time, and in order to handle
different scenes that may hold different content we may need smooth transitioning
between them. The following is a code sample that will clear things up for you,
add it to
ERGMyScene.m
:
- (void) gameOver
{
ERGGameOverScene *newScene = [[ERGGameOverScene alloc]
initWithSize:self.size];
SKTransition *transition = [SKTransition
flipHorizontalWithDuration:0.5];
[self.view presentScene:newScene transition:transition];
}