1

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?

Skiny
  • 405
  • 1
  • 4
  • 16

4 Answers4

4

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;
}
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • Thanks, I'm doing exacly that but let's say I need to attach physics body only to the "head" SKSpriteNode and that the "head" moves the entire "controller". Not that the "head" just leaves the container responsing to gravity, but I need it to "take" the "controller" with it. Hope you know what I mean :) – Skiny Oct 18 '13 at 13:15
  • Oh my, didSimulatePhysics... I totally forgot that. Thanks a bunch! – Skiny Oct 18 '13 at 13:39
0

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;
Community
  • 1
  • 1
DogCoffee
  • 19,820
  • 10
  • 87
  • 120
  • Thanks for the response, Smick. Yes, I understand that, but I didn't explained what I want as precisly as I wanted. I want the character (containing all the child sprites) to follow the child sprite with the body. Sry for my english, hope you got my point now :) – Skiny Oct 18 '13 at 13:10
0

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.

user2277872
  • 2,963
  • 1
  • 21
  • 22
-1

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

Maq
  • 369
  • 3
  • 13
  • But I need the SKPhysicsBody to be in a child node not in the root node of my character – Skiny Oct 18 '13 at 13:18