Game Development Reference
In-Depth Information
//Pinning Rectangle and Square
NSLog(@"Head position %@",
NSStringFromCGPoint(head.position));
SKPhysicsJointPin* pin =[SKPhysicsJointPin
jointWithBodyA:backBone.physicsBody
bodyB:head.physicsBody
anchor:CGPointMake(head.position.x-5,
head.position.y)];
[scene.physicsWorld addJoint:pin];
}
In the first five lines of code, we are creating a rectangular sprite with a physics
body. We have also specified the collision and the category mask for this sprite.
Similarly, in the following lines of code, we will create square sprite with the
physics body attached. For this sprite also, we are specifying the category and the
collision masks.
Then, finally, in last three lines of code, we are attaching the two bodies to each
other through a pin joint. We've created an object of the SKPhysicsJointPin
class and provided both the rectangle and square bodies to it along with the an-
chor point from which they will be rotating around each other.
2. Now, replace the createPhysicsBodiesOnScene function call with cre-
atePinJointOnScene . Add the following code at the end of the init func-
tion:
[self createPinJointOnScene:self];
The final function should look something similar to following screenshot:
3. Now, compile and run the project and you should be able to see both the physics
bodies attached with the pin joint. You can see them attached to each other
through the anchor point.
Search WWH ::




Custom Search