0

Here is my code:

    // setting background
_ground = [[SKSpriteNode alloc]
                         initWithColor:[SKColor greenColor]
                                  size:CGSizeMake([Helpers landscapeScreenSize].width, 50)];
_ground.name = @"ground";
_ground.zPosition = 1;
_ground.anchorPoint = CGPointMake(0, 0);
_ground.position = CGPointMake(0, 0);
_ground.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake([Helpers landscapeScreenSize].width, 50)];
_ground.physicsBody.dynamic = NO;
_ground.physicsBody.affectedByGravity = NO;
_ground.physicsBody.categoryBitMask = groundCategory;
_ground.physicsBody.collisionBitMask = elementCategory;
_ground.physicsBody.contactTestBitMask = elementCategory;

Green rectangle positioning is ok, but SKPhysicsBody is not representing this rectangle correctly. It looks like SKPhysicsBody is not "moving" its body according to sprite position and anchorPoint. SKPhysicsBody is moved left for _ground.width points.

What am I doing wrong?

PS. Changing to this (code) solved my problem, but I really dont like this solution, it seems an ugly way to position an element.

_ground.position = CGPointMake([Helpers landscapeScreenSize].width / 2, 0);

+removing:

_ground.position = CGPointMake(0, 0);
AndrewShmig
  • 4,843
  • 6
  • 39
  • 68

1 Answers1

1

I had exactly the same issue yesterday ;) For me I solved this by using the default sprite positioning by SpriteKit. I just set the position and leave the anchor point at it's default. Then you set the physicsbody to the size of the sprite.

Change your code to the following and check if it works:

_ground = [[SKSpriteNode alloc]
                         initWithColor:[SKColor greenColor]
                                  size:CGSizeMake([Helpers landscapeScreenSize].width, 50)];
_ground.position = CGPointMake([Helpers landscapeScreenSize].width / 2, 0);
_ground.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_ground.size];

If you still have no luck with this you can also try to use another initialisation for your sprite (this works in my project). Try it with a texture:

[[SKSpriteNode alloc] initWithTexture:[SKTexture textureWithCGImage:[UIImage imageWithColor:[UIColor whiteColor]].CGImage] color:[UIColor whiteColor] size:CGSizeMake(PLAYER_WIDTH, PLAYER_HEIGHT)];

don't forget to implement the (UIImage*)imageWithColor: method to create a 1x1 pixel image of your desired color for you (see here).

Community
  • 1
  • 1
E. Lüders
  • 1,495
  • 1
  • 12
  • 27
  • It works, but thats not the question. Why moving anchorPoint to (0,0) and adding SKPhysicsBody makes incorrect positioning of this SKPhysicsBody? – AndrewShmig Mar 01 '14 at 20:32
  • I think there are some bugs in SpriteKit. I also encountered some other issues with the position of the physicsBody when I change size or position of a Node. – E. Lüders Mar 01 '14 at 20:34
  • 2
    anchorPoint is a purely visual property: it defines how the texture is drawn relative to node position. It has no effect on bodies. This is not a bug. – CodeSmile Mar 02 '14 at 07:01
  • @LearnCocos2D, so, the right way to do positioning of SKPhysicsBody - don't change anchorPoint from (0.5, 0.5) to (0,0) and reposition the whole spriteNode? – AndrewShmig Mar 02 '14 at 08:39
  • @LearnCocos2D, thank you, please post your comment as an answer to my q and I'll accept it. – AndrewShmig Mar 07 '14 at 04:51