i am using Spritekit and want to have a hit test, that works intuitive. In other words: I got a position x,y and want to to find out which is the top most node that was hit. (And with "top most" i mean the one that the user actually sees as topmost.)
I did not find any predefined functionality of SpriteKit that gives me this functionality. Actually i found that SpriteKit itself uses some counter intuitive hit testing, see here.
My idea to implement this by myself is to calculate some global z ordering, like described in the answer here, by Dobroćudni Tapir.
I think this may work, what confuses me is the statement in the documentation:
Important: The SKCropNode and SKEffectNode node classes alter the scene rendering behavior. The children of these nodes are rendered independently as a separate node tree, and the results are rendered into the tree that contains the crop or effect node. For more information, see Working with Other Node Types.
To find here, beneath "Understanding the Drawing Order for a Node Tree".
I did not find the referred "Working with Other Node Types" point, so i have no idea how and why SKCropNodes or SKEffectNodes should change the drawing order ...
So answers to the following questions could help me:
- Does SpriteKit offer some functionality to retrieve the drawing oder?
- If not, do you think that my custom implementation work? (How could Crop or Effect nodes destroy it?)
- Could you point me to information regarding this question? Like the missing "Working with other nodes" article?
Update:
I just found out that the behaviour reported here is not correct.
As mentioned in the comments i was pointed to nodeAtPosition or nodesAtPosition. I used the code in here and tested it.
The result is not satisfying. Two problems:
- The set zPosition is ignored in nodesAtPosition. Independent if blue or red square is in front i get the same order in the array returned in nodesAtPosition.
- These functions use the accumulated frame. But in a prefect case i would like to use the exact size of each node and not the accumulated frame.
Some pictures to 1. :