Game Development Reference
In-Depth Information
How to do it…
Now, we will open our working project again to implement an example dealing with colli-
sion and contact detection. The following steps will provide the step-by-step ways to im-
plement and understand collision detection in our project:
1. To implement the collision detection, open the GameScene.m file and add the
following function at the end of the file:
-
(void)createCollisionDetectionOnScene:(SKScene*)scene {
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];
SKSpriteNode* backBone = [[SKSpriteNode alloc]
initWithColor:[UIColor whiteColor] size:CGSizeMake(20,
200)];
backBone.position =
CGPointMake(CGRectGetWidth(self.frame)/2.0,
CGRectGetHeight(self.frame)/2.0);
backBone.physicsBody = [SKPhysicsBody
bodyWithRectangleOfSize:backBone.size];
backBone.physicsBody.categoryBitMask =
GFPhysicsCategoryRectangle;
backBone.physicsBody.collisionBitMask =
GFPhysicsCategorySquare;
backBone.physicsBody.contactTestBitMask =
GFPhysicsCategorySquare;
backBone.physicsBody.dynamic = YES;
[scene addChild:backBone];
Search WWH ::




Custom Search