Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

ios - SKSPriteNode position changes to 0,0 for no reason

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...