2

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:

  1. 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.
  2. 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. : enter image description here

enter image description here

Community
  • 1
  • 1
NieLernend
  • 190
  • 9
  • The SK Programming Guide explicitly states that hit testing is done in reverse draw order. That seems to be both what you want and well defined. – trojanfoe Apr 21 '15 at 14:49
  • Do you have any references for that? What i have found suggested that this is not the case, [see](http://stackoverflow.com/questions/24245494/in-spritekit-how-is-zposition-used-for-rendering-and-for-hit-tests). And if i am wrong here. Can you point a function in SpriteKit where i can perform a hit test explicitly for a position? – NieLernend Apr 21 '15 at 15:17
  • To what end is this critical for your code logic? – sangony Apr 21 '15 at 15:28
  • The point is that i get events from hardware that i need to process into the scene. At this point i need to decide to which of the nodes i refer the event. Therefore i need to now which node is at position x,y at the top of the drawing order. The Problem is i did not find anything to get this draw order or to ask SpriteKit something like "which is top node at position x,y" – NieLernend Apr 21 '15 at 17:09
  • @NieLernend See [this](https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Nodes/Nodes.html) (search for section entitled **The Hit-Testing Order Is the Reverse of Drawing Order**). – trojanfoe Apr 22 '15 at 06:41
  • @trojanfoe thanks for the hint. I tested both of these functions and found two problems. Updated my question. – NieLernend Apr 22 '15 at 09:01

0 Answers0