Question

In my TouchesBegan method of SKScene I am checking if the user has selected a node as so:

UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
NSLog(@"node.name: %@",node.name);

if ([node.name isEqualToString:@"1"]){
    bgColor = 1;
}
else if ([node.name isEqualToString:@"2"]){
    bgColor = 2;
}
else if ([node.name isEqualToString:@"3"]){
    bgColor = 3;
}
else if ([node.name isEqualToString:@"4"]){
    bgColor = 4;
}
else if ([node.name isEqualToString:@"5"]){
    bgColor = 5;
}
else if ([node.name isEqualToString:@"6"]){
    bgColor = 6;
}

My nodes are all initialized in method like so:

SKShapeNode *bg = [SKShapeNode node];
[bg setPath:CGPathCreateWithRoundedRect(CGRectMake(self.size.width-50*2, 55, 50, 50), 6, 6, nil)];
bg.fillColor = [SKColor orangeColor];
bg.glowWidth = 0;
bg.name = @"6";
return bg;

The methods are all similar, except for the name and color properties, of course. The nodes are divided in two lines, one at y 120 and the other at y 55, the Xs are different, but when the user clicks any node in the line 120 or 55 the node name logged is the one from the node at the most right. Why is that happening? And what am I doing wrong?

Was it helpful?

Solution

I could recreate you problem and found the solution:

Don't position the SKShapeNode by setting the start and end point in CGRectMake(). Instead, just use the regular postion property of the SKShapeNode. Otherwise, the nodes seem to overlap each other, although the shapes are at different coordinates.

SKShapeNode *bg = [SKShapeNode node];
[bg setPath:CGPathCreateWithRoundedRect(CGRectMake(0, 0, 50, 50), 6, 6, nil)];
bg.position = CGPointMake(self.size.width-50*2, 55);
bg.fillColor = [SKColor orangeColor];
bg.glowWidth = 0;
bg.name = @"6";
return bg;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top