Game Development Reference
In-Depth Information
3. node2
4. node1
Notice the last node rendered was the first node added to the scene. Nested nodes are also
rendered in the reverse order they were added. If you were to render the node tree repres-
ented by Figure 2-5 , it would be rendered in the following order:
1. node3
2. node4
a. node4c
b. node4b
c. node4a
3. node1
This is important because, based upon the position of each node, there could be some
overlapping of nodes in the scene, which could lead to partial or completely hidden nodes.
It is also important because of the way Sprite Kit performs hit testing. When Sprite Kit
processes a touch event or mouse event, it walks the scene to find the closest node that
wants to receive the event. If the first node doesn't want the event, Sprite Kit checks the
next closest node and repeats this process until the event is handled or ignored. Just like
the scene rendering order, the order in which hit testing is performed is the reverse of the
drawing order.
To see how this looks using the sample app, replace the current contents of the GameS-
cene.init() method with the following code:
super.init(size: size)
backgroundColor = SKColor(red: 0.0, green: 0.0, blue: 0.0,
alpha: 1.0)
// adding the background
backgroundNode = SKSpriteNode(imageNamed: "background")
backgroundNode!.anchorPoint = CGPoint(x: 0.5, y: 0.0)
backgroundNode!.position = CGPoint(x: size.width / 2.0, y:
0.0)
addChild(backgroundNode!)
// add the player
Search WWH ::




Custom Search