-1

I have an object which inherits SKNode where I set its position to a certain point. I add the node to a layer and its position is still set to the same point. The node appears at (0,0) and later on in the completion block of one of its actions I see its position was changed to (0,0).

The SKNode object has 1 child (particle emitter) whose position is (0,0). (Note the position of the particle emitter should be (0,0) and the node itself (emitter's parent) should be at whatever position I'm setting it to.

Any ideas why this might be happening?

EDIT:

I added

- (void)setPosition:(CGPoint)position
{
    [super setPosition:position];
}

to my class and put a breakpoint there. The breakpoint gets hit the first time when I set the position, but never gets hit again. Somehow the position is still (0,0)...

EDIT 2:

SKNode position not working, always going to default 0,0

This post describes my exact problem.

Community
  • 1
  • 1
NMunro
  • 1,282
  • 4
  • 18
  • 33
  • Can you post the code you're using to move the node? – Abhi Beckert Feb 14 '14 at 01:34
  • I have a missile object that contains an explosion object. self.explosion.position = self.position; After executing that line the explosion has the right position, but it is drawn at (0,0) – NMunro Feb 14 '14 at 01:36
  • I think its something to do with physics. I use the same code elsewhere and it works correctly, the only difference being that the other object doesn't have a physics body, whereas this one does. – NMunro Feb 14 '14 at 01:39
  • Show us all the lines of code where you apply the setPosition method to a node. – El Tomato Feb 14 '14 at 01:41
  • I never call set position, I just use self.explosion.position = self.position – NMunro Feb 14 '14 at 01:47
  • It seems if I set the position within my - (void)didBeginContact:(SKPhysicsContact *)contact method, then the position ends up always being (0,0). – NMunro Feb 14 '14 at 01:51
  • @NMunro I think you're doing something wrong, but impossible to help if you don't post the code that you're using. – Abhi Beckert Feb 14 '14 at 04:43
  • It's apparently a bug with sprite kit, or some misunderstanding with physics. Read the post I linked above. – NMunro Feb 14 '14 at 05:06

1 Answers1

0

The solution I found isn't perfect, but its working for now.

What I'm doing is setting the physics body after I set the position and add the node as a child.

I added a method to my explosion called addPhysics which sets the physics body.

self.explosion.position = self.position;
[self.gameDelegate addNodeToLayer:self.explosion];
[self.explosion addPhysics];
NMunro
  • 1,282
  • 4
  • 18
  • 33