6

I create an SKSpriteNode in didBeginContact method with location of contact:

CommonBox *boxTwo = [[CommonBox alloc] initWithTexture:[self.mainAtlas textureNamed:@"box07"]];
boxTwo.name = @"regularBox";
boxTwo.position = contact.contactPoint;
[self addChild:boxTwo];

The position is correct here. Then update method is called and the position of box is magically changed to 0,0 at start of update method, before any code at all is executed. Why is that? No other code is executed between these two events. This is driving me crazy. I tried removing all collision and contact bit masks from this object, but it changes to zeroes anyway. Object is legal and so on.

EDIT: if I remove physics body from that node I will get desired position. But how do I set my position of node if it has physics body?

Dvole
  • 5,725
  • 10
  • 54
  • 87

1 Answers1

7

I found out my mistake - you have to create physics body after you set node position. Setting position after physics body has position doesn't do anything.

Dvole
  • 5,725
  • 10
  • 54
  • 87
  • I just ran into this myself and suspect it is a bug as it only happens inside the update method. When creating my objects during scene setup it doesn't matter what order I set the position in. Did you file a radar with Apple? – macshome Jan 19 '14 at 01:11
  • i came across this as i have created a spritenode as subclass , & i have created also as SKspritenode inside the scene , when i init the subclass sprite node outside the scene init function , the position is 0,0 , when i init the SKspritenode created whithin the Scene it is OK, This does not not make any sense ????? – TALAA Jan 29 '14 at 21:05
  • 2
    Thank you, I wish I found this earlier. In my case it seems inconsistent. Sometimes my subclassed SKSpriteNodes can given positions after the physicsBody has been created, and others not. At the moment I don't see any differences between those that work and those that don't, and will be chalking this up as a probable bug with the framework. – Koop Feb 23 '14 at 19:22
  • I'm experiencing the same issue as Koop: in most situations, setting the position after creating the physicsBody works, but in some cases it doesn't. Have you guys found any new information about this since your last posts? – harrisonlee Mar 18 '14 at 23:52
  • No new information, other than I have now experienced this in a slightly different scenario. I ran into an issue where setting the position of an SKSpriteNode was again being ignored. This happened after introducing a world node into my scene in order to center the scene on a node. I found that I was only able to get the position working if I set the physics body after I added the node the the world, but not before. – Koop Mar 20 '14 at 06:37
  • Ok cool. Thanks for the reply. This seems inconsistent to me, but perhaps there's something about SpriteKit that I'm missing. I've posted a question in the Apple Developer Forum. Haven't heard back yet, but you can check it out here: https://devforums.apple.com/thread/222332. I'll be sure to post back here if I get any new info. – harrisonlee Mar 21 '14 at 17:05