Game Development Reference
In-Depth Information
Node . Take a look at the following removeOutOfSceneNodesWithName() meth-
od:
func removeOutOfSceneNodesWithName(name: String) {
foregroundNode!.enumerateChildNodesWithName(name,
usingBlock: {
node, stop in
if self.playerNode == nil {
stop.memory = true
}
else if (self.playerNode!.position.y
- node.position.y > self.size.height)
{
node!.removeFromParent()
}
})
}
This method takes a single String representing the name of each SKNode you want to test
and uses the enumerateChildNodesWithName() method to check to see whether
those nodes are positioned one scene length below the playerNode . If the returned
node is greater than one scene length, then it is removed from the scene. Notice one thing
about this method. Inside the enumerateChildNodesWithName() method it first
checks to see whether the playerNode is nil . If the playerNode is nil , then it
stops the search for child nodes by setting the stop.memory property to true . Add
this method to the bottom of the GameScene .
To use the removeOutOfSceneNodesWithName() , you need to add a call to this
method, for each node that can be removed from the scene, to the bottom of GameS-
cene 's update() method. In this instance, the two nodes that can fall out of the scene
are the BLACK_HOLE and POWER_UP_ORB nodes. Take a look at the modified up-
date() method with these two calls added to the end of the method, shown here:
override func update(currentTime: NSTimeInterval) {
if self.playerNode != nil {
if self.playerNode!.position.y >= 180.0 &&
self.playerNode!.position.y < 6400.0 {
self.backgroundNode!.position =
CGPointMake(self.backgroundNode!.position.x,
Search WWH ::




Custom Search