SKNode position does not work, always defaults to 0.0

I have a pretty well-developed SpriteKit game that I'm working on, I added code to add a new SKSpriteNode script to the scene (just like I did dozens of other places in the code, but for some reason this time just ignoring the position that I installed for node, I tried it with explicit .position = CGPointMake (x, y) and tried [node setPosition: CGPointMake (x, y)]

None of them work ... node is added to the scene, but ALWAYS at 0.0, no matter what I do, checked both x and y to make sure they are valid and are in the range of the scene and tried use explicit floats, etc., and it doesn’t matter what I do, this node is always mapped to 0.0, and this is completely pointless. I do the same thing as I do with dozens of other nodes during the game, but this node just will not appear anywhere other than 0.0, regardless of what I do:

int width = 64; SKSpriteNode *node = [[SKSpriteNode alloc] initWithColor:[SKColor grayColor] size:CGSizeMake(width,25)]; node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:node.size]; node.physicsBody.dynamic = NO; node.physicsBody.categoryBitMask=nodeCategory; node.name=@ "node"; node.position = CGPointMake(200,250); //Have also tried [node setPosition:CGPointMake(200,250); [self addChild:node]; 

I am completely at a loss here, I tried adding .0 to the inputs in CGPointMake to make sure they are processed, since nothing is floating ... No matter what I do with this node, when it is added to the scene it is always by default takes a 0,0 position, and I'm upset, I add all kinds of nodes without incident, just something about this node addition that just doesn't work, and can't figure out what is different or why.

+6
source share
1 answer

I have the same problem. I have a subclass of SKSpriteNode, which is used by most of my game nodes as a superclass. I added this method, which I use whenever I encounter a positioning problem:

 - (void)refreshPhysicsBodyAndSetPosition:(CGPoint)position { /* * Weird thing here: if I just set the position of these nodes, they * end up at position (0,0). However, if I remove the physics body, set * the position, and then re-add the physics body, the nodes are * placed correctly. */ SKPhysicsBody *tempPhysicsBody = self.physicsBody; self.physicsBody = nil; // Position and re-add physics body self.position = position; self.physicsBody = tempPhysicsBody; } 

Obviously, this is not a real answer to this question, but it is a workaround that may help some of you. I have a question open on the Apple Developer forum, which you can see here:

https://devforums.apple.com/thread/222332

I hope I will be back soon.

+3
source

All Articles