Game Development Reference
In-Depth Information
SKLabelNode* collisionLabel;
9. Now, add the following lines of code at the start of the createColli-
sionDetectionOnScene function:
collisionLabel = [SKLabelNode
labelNodeWithFontNamed:@"Futura-Medium"];
collisionLabel.text = @"Collision detected";
collisionLabel.fontSize = 18;
collisionLabel.fontColor = [SKColor whiteColor];
collisionLabel.position =
CGPointMake(CGRectGetWidth(self.frame)/2.0,
CGRectGetHeight(self.frame)/1.2);
collisionLabel.alpha = 0.0f;
[scene addChild:collisionLabel];
10. Now, for fade-in and fade-out of the label on collision, add the following line of
code at the end of the didBeginContact method:
SKSpriteNode *firstNode, *secondNode;
firstNode = (SKSpriteNode *)contact.bodyA.node;
secondNode = (SKSpriteNode *) contact.bodyB.node;
if (firstNode.physicsBody.categoryBitMask ==
GFPhysicsCategoryRectangle &&
secondNode.physicsBody.categoryBitMask ==
GFPhysicsCategorySquare) {
SKAction *fadeIn = [SKAction fadeAlphaTo:1.0f
duration:0.2];
SKAction *fadeOut = [SKAction fadeAlphaTo:0.0f
duration:0.2];
[collisionLabel runAction:fadeIn completion:^{
[collisionLabel runAction:fadeOut];
}];
}
11. Here we are checking for the rectangular and square bodies. Once we get a call-
back for the collision of these two bodies, we can fade in the label for a fraction
Search WWH ::




Custom Search