sprite-kitskspritenodetoucheszposition

How to stop SKSpriteNodes changing zPosition on touch


I am having an issue with the zPosition undesirably changing when a node is touched.

I can't give a simple code example because currently my scene and rendering is quite complex. Everything is drawn correctly when the scene is presented and for the most part besides as described below it works.

So the issue explained - we have a base image node correctly rendered below a tower node (tower defence game). The tower is circular and the base image is square, so the corners stick out. If you manage to touch the corners, for some reason beyond my knowledge, the zPosition changes to above the tower.

There is no particular code on touches to change any zPositions of anything in the game.

I know this is a shot in the dark, but any insight would be great and much appreciated.


Solution

  • If your view's ignoreSiblingOrder property is set, you should set the zPosition of each node, appropriately, to ensure that they are drawn in the correct order.

    From Apple's documentation...

    The default value (of SKView's ignoreSiblingOrder property) is NO, which means that when multiple nodes share the same z position, those nodes are sorted and rendered in a deterministic order. Parents are rendered before their children, and siblings are rendered from eldest to youngest. When this property is set to YES, the position of the nodes in the tree is ignored when determining the rendering order. The rendering order of nodes at the same z position is arbitrary and may change every time a new frame is rendered [emphasis added].