Game Development Reference
In-Depth Information
//Adding Square
SKSpriteNode* head = [[SKSpriteNode alloc]
initWithColor:[SKColor grayColor] size:CGSizeMake(40,
40)];
head.position = CGPointMake(backBone.position.x,
backBone.position.y+backBone.size.height/2.0+40);
head.physicsBody = [SKPhysicsBody
bodyWithRectangleOfSize:head.size];
head.physicsBody.categoryBitMask =
GFPhysicsCategorySquare;
head.physicsBody.collisionBitMask =
GFPhysicsCategoryRectangle;
head.physicsBody.contactTestBitMask =
GFPhysicsCategoryRectangle;
head.physicsBody.dynamic = YES;
[scene addChild:head];
[NSTimer scheduledTimerWithTimeInterval:5
target:self selector:@selector(applyImpulseUpwards:)
userInfo:@{@"body":head.physicsBody,@"impulse":@(50)}
repeats:YES];
}
Now we are familiar with code in this function. We will create two physics bodies
and add them on the scene. Finally, in the last section, we will apply an impulse
every 5 seconds on the physics body.
Here we are updating three additional parameters for each of the physics bodies.
We are updating categoryBitMask , collisionBitMask , and con-
tactTestBitMask for the bodies. As explained, we are updating cat-
egoryBitMask to provide the specific categories to the physics bodies. Along
with that, we provide the information to define which bodies it can detect colli-
sion with.
2. Now we have to add the delegate method, which will be invoked when both the
bodies collide with each other. We have added logs to check the bodies that are
colliding. We can identify bodies using their categoryBitMask .
Search WWH ::




Custom Search