Search code examples
objective-csprite-kitcollision-detectionskspritenodeskphysicsbody

SpriteKit - The child of my node does not appear to be changing its zPosition attribute


my issue is, I've got a "2.5D" game where characters that come into contact with one another need to dynamically change their zPosition. I'm using physics bodies to achieve this.

It works OK with the characters, however, the shield they are holding (a child object) is not following suit even though I'm logging a correct change of zPosition attribute.

if (((firstNode.categoryBitMask & dudeCategory) != 0) && ((secondNode.categoryBitMask & shieldGuyCategory) !=0))
{
    if ((firstNode.node.position.y < secondNode.node.parent.position.y))
    {
        [secondNode.node.parent enumerateChildNodesWithName:@"shieldNode" usingBlock:^(SKNode *node, BOOL *stop){
            NSLog(@"Ooh I Say! Shield zPos = %f", node.zPosition);

            SKAction *zPos = [SKAction runBlock:^{
                node.zPosition = node.zPosition -200;

                NSLog(@"And now, %@ zPos = %f", node.name, node.zPosition);
                NSLog(@"But static NPC zPos = %f", firstNode.node.zPosition);
                NSLog(@"And finally, Shield-holder Guy zPos = %f", secondNode.node.parent.zPosition);
            }];

            [node runAction:zPos];
        }];

        secondNode.node.parent.zPosition = secondNode.node.parent.zPosition -200;
    }
}

Halp!


Solution

  • So, it turns out I was setting the zPosition for the shield in its method. Removing that restores normal behaviour when the parent changes zPosition, having it declared explicitly seems to override the nested behaviour.