I add a couple of nodes, like this:
SKShapeNode* node= [SKShapeNode shapeNodeWithRectOfSize:rect.size]; // always 32x32
node.fillColor= [UIColor darkGrayColor];
node.strokeColor= [UIColor lightGrayColor];
node.position= position;
node.physicsBody= [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(rect.size.width - 2.0, rect.size.height - 2.0)];
node.physicsBody.categoryBitMask= catSomeNode;
node.physicsBody.contactTestBitMask= catSomeOtherNode;
node.physicsBody.dynamic= YES;
node.physicsBody.usesPreciseCollisionDetection= YES;
node.physicsBody.affectedByGravity= NO;
node.physicsBody.collisionBitMask= 0;
[self addChild:node];
NSLog(@"Adding node at position %@", NSStringFromCGPoint(node.position));
e.g. for the first shape:
2015-11-14 16:17:25.162 MyApp[13479:632495] Adding node at position {16, 16}
Later, I will try to animate the shape when it is touched. To determine the direction of the animation, I need to find out the position of the node.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode* node = [self nodeAtPoint:location];
if (!(node.physicsBody.categoryBitMask & catSomeNode))
return;
if ([node isKindOfClass:[SKShapeNode class]])
{
// Here, the position is different
}
}
The changed position according to the debugger is:
(lldb) p position
(CGPoint) $0 = (x = 15.999996185302734, y = 15.999996185302734)
This will mess up my logic, because i cannot easily compare positions.
2 questions:
1) Positioning
Is it correct, that positioning is always based on the node center, unlike e.g. UIView
? This means essentially when I want to place a node at 0/0, I will have to set the position to width/2 / height/2, right?
This feels a bit cumbersome, but well, I can work with that.
2) Touch detection
As you can see in my example, the position of the brick was changed. Why is that? After [self addChild:node];
the position will remain at 16/16, but after the touch detection it is changed to 15.999996185302734 / 15.999996185302734.
Whats wrong? I even saved the pointer of the node as class member, to check the position (in case nodeAtPoint
returns a different object). But still the position is messed up.
[edit] Did some more tests. I saved the pointer to the SKShapeNode
added above in my class, and I put a debug message in the scene's update:
method, which prints the position of the node. This is what happens:
2015-11-14 18:39:07.560 MyApp[14414:696141] Node position {16, 16}
2015-11-14 18:39:07.701 MyApp[14414:696141] Node position {15.999999046325684, 15.999999046325684}
2015-11-14 18:39:07.866 MyApp[14414:696141] Node position {15.999998092651367, 15.999998092651367}
2015-11-14 18:39:07.951 MyApp[14414:696141] Node position {15.999997138977051, 15.999997138977051}
2015-11-14 18:39:08.019 MyApp[14414:696141] Node position {15.999996185302734, 15.999996185302734}
There is no code executed between the first update:
and the second one. Also I did not tap, so no touch detection was done.
This leads me to believe I have some sort of initialization issue, so the scene is somewhat changed as soon as the app is loaded, or the scene becomes visible. I thought I already have this covered, because the node is added in the - (void)didMoveToView:(SKView *)view
method.