Cocorico
Cocorico

Reputation: 2347

How can I "pin" sprites to other sprites in Sprite Kit?

So I have a marionette I want to display in Sprite Kit. The marionette is made of a bunch of different body parts, each of which is a .png of course.

So my process is: I have a Marionette object (SKNode subclass). I add the head to this node. Now, I add the chest to the head. It works fine, but if I add physics, and make the 2 images fall to the ground, they stick together until the chest (which is lower than the head) hits the ground. The chest stops, and the head keeps falling and separates. What I want is to have the body parts stick together.

here is the code I'm using to attach the chest to the head, any thoughts on how I can achieve this?

self.chest = [SKSpriteNode spriteNodeWithImageNamed:@"chest_neck.png"];
self.chest.anchorPoint = CGPointMake(0.5, 0.0);
self.chest.position = CGPointMake(4, -135);
[self.head addChild:self.chest];
self.chest.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:20];
self.chest.physicsBody.mass = 1;
self.chest.physicsBody.dynamic = YES;

Upvotes: 1

Views: 1028

Answers (1)

prototypical
prototypical

Reputation: 6751

You need to use a SKPhysicsJoint.

There are several types, you can find more information in Apple Developer Library here :

https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKPhysicsJoint_Ref/Reference/Reference.html

Upvotes: 2

Related Questions