Skiny
Skiny

Reputation: 405

SpriteKit: SKSpriteNode containing another SKSpriteNodes but only one SKPhysicsBody

I have a character in my game, it's an SKSpriteNode with few child SKSpriteNodes so I can animate various parts of my character (hands, feet etc.), it also has 1 SKSpriteNode (tried replacing with SKNode, but it was the same) with SKPhysicsBody for a body.

When I add the character to my layer in a scene it just hang in the position and the sprite with the body falls down.

My question and problem is: How can I keep all the child sprites in my main character sprite - how can I keep my character together?

Thanks for any ideas!

EDIT: How can I keep the child sprite with the body attached to my container sprite?

Upvotes: 1

Views: 4036

Answers (4)

CodeSmile
CodeSmile

Reputation: 64477

Re EDIT:

Use an SKNode to control all of your character's body parts, including the main body:

SKNode (controller)
  SKSpriteNode (head)
  SKSpriteNode (body)
  SKSpriteNode (leg1)
  SKSpriteNode (leg2)

That gives you more flexibility.

To make the head the "master" position, this should do the trick:

-(void) didSimulatePhysics
{
    self.parent.position = [self convertPoint:self.position toNode:self.parent.parent];
    self.position = CGPointZero;
}

Upvotes: 4

user2277872
user2277872

Reputation: 2973

If I am understanding you right, you can start off by saying the gravity on the SKScene is 0, by doing

    self.physicsWorld.gravity = CGVectorMake(0,0);

This will ensure that nothing causes the character of your screen to fall down into the bottom from the gravity enforced on the screen.

Upvotes: 0

DogCoffee
DogCoffee

Reputation: 19946

The body is falling because its responding to gravity, as expected. The Other body part, if they do not have physics bodies will stay where you place them.

Some options :

1.

If you want all body parts to be physics body then I suggest looking at a joints, ie a pin joint - which will work like a shoulder joint for example.

Here is an example of pin joints for making a car with wheels.

2.

If you just want it not move, for whatever reason. On your physics body, just set this.

bodyNode.physicsBody.dynamic = NO;

Upvotes: 0

Maq
Maq

Reputation: 403

Possible solution: remove SKPhysicBody from child node and add it to parent node.

Upvotes: -1

Related Questions