1
votes

I'm creating an iOS app with Sprite Kit & I'm trying to select (onTouchBegan) a SKNode that lies below another SKNode. The structure is : FirstLayer(SKNode) - SecondLayer(SKNode) & 3rd Layer (SKNode). I'm trying to select the SecondLayer and move it around onTouchMove. The current problem is that now touch recognises the 3rdLayer. Can you help me ?

1
use if (zPosition == x) {}Scott
can u please give me more details ?alexsc

1 Answers

2
votes

To elaborate on Scott's comment, the zPosition property of any SKNode is essentially the layer it lies on, with the higher layer numbers being "closer" to the front of the screen. Therefore if you want to have nodeB on top of nodeA, then nodeA would have a zPosition with a higher number than that of a, such as:

nodeA.zPosition = 1
nodeB.zPosition = 2

If you set the zPosition of a node when you create it, you can set a conditional check on the zPosition of a touched node in the 'touchesBegan' method. Following the above example, if you would like to touch nodeA and notnodeB, you would check that the zPosition of the node being touched is equal to 1:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];

    if(node.zPosition == 1){
        //do stuff to nodeA here
    }
}

I think this is what Scott was getting at, I hope this helps.