5

SKNode has an anchorpoint property and it behaves as I expect. However, if I change an SKNode's anchorpoint from its default (which is (0.5,0.5)), the physicsbody for the SKNode becomes unaligned from what is displayed in the scene for the SKNode.

I'm on IOS 7.

Is there any way to fix this so that I can use non-default values of SKNode.anchorpoint without causing a misalignment between the displayed SKNode and the SKNode.physicsbody?

Here's an example. In an SKScene.didMoveToView() implementation, add this code:

// Set up the scene.
self.anchorPoint = CGPointMake(0.5, 0.5);
self.backgroundColor = [SKColor yellowColor];
self.scaleMode = SKSceneScaleModeAspectFill;

// BLOCK
SKSpriteNode *block = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(50, 50)];
block.name = @"Block";
block.anchorPoint = CGPointZero;
block.position = CGPointMake(0., 0.3*self.size.height);

block.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize: block.size];

[self addChild: block];

// FLOOR
SKSpriteNode *floor = [SKSpriteNode spriteNodeWithColor: [SKColor blueColor] size:self.size];

floor.name = @"Floor";

floor.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize: floor.size];
floor.physicsBody.dynamic = NO;

floor.position = CGPointMake(0., -0.9*self.size.height);

[self addChild: floor];

When I run this code, the block falls to the floor, but it comes to rest well above the floor because there's a misalignment between its physicsbody and its sprite. If I comment out the line setting the block's anchorpoint, everything works fine.

Will Nelson
  • 966
  • 8
  • 13
  • Why do you need to set the block node's anchorpoint to zero? Also, try setting the anchor point after you initialise the physicsBody – ZeMoon Mar 06 '14 at 05:36
  • anchorpoint is a purely visual property, search for it on SO, I've explained this multiple times in more detail and workarounds – CodeSmile Mar 06 '14 at 08:19
  • 1
    This question has a good solution here: http://stackoverflow.com/questions/20045203/physicsbody-doesnt-adhere-to-nodes-anchor-point/20046199#20046199 – ZeMoon Mar 11 '14 at 13:34
  • 1
    Also, this issue has been resolved in iOS 7.1. A couple of new methods have been implemented for that. – ZeMoon Mar 11 '14 at 13:41
  • 1
    @WillNelson SKNode doesn't have an anchor point (nor SKEmitterNode, SKEffectNode, SKLabelNode). SKSpriteNode does, as well as SKScene. – Whirlwind Aug 21 '16 at 17:38

0 Answers0