Game Development Reference
In-Depth Information
The purpose of this node will be to represent a black hole moving back and forth across
the scene. If the player node comes into contact with this node, the player will stop re-
sponding to the physics world and slowly fall to their death.
To add this new node to the scene, you need to create a new
SKSpriteNode
instance
and pass it the
BlackHole0
image. You can find the code to do this in the following
method, named
addBlackHolesToForeground()
:
func addBlackHolesToForeground() {
var blackHoleNode = SKSpriteNode(imageNamed:
"BlackHole0")
blackHoleNode.position = CGPointMake(size.width - 80.0,
600.0)
blackHoleNode.physicsBody =
SKPhysicsBody(circleOfRadius:
blackHoleNode.size.width / 2)
blackHoleNode.physicsBody!.dynamic = false
blackHoleNode.name = "BLACK_HOLE"
foregroundNode!.addChild(blackHoleNode)
}
You have seen all this code before. It starts by creating a new instance of the
black-
HoleNode
using the image named
BlackHole0
. It then adds the node to the scene,
sets the properties of the node's
physicsBody
, and names the node
BLACK_HOLE
. Go
ahead and add this code immediately following the
addOrbsToForeground()
meth-
od. And then add a call to invoke the
addBlackHolesToForeground()
method
right before the invocation of the
addOrbsToForeground()
method.
After you have made these changes, run the app again. This time you will see the
blackHoleNode
suspended up and to the right of the player node, as shown in
Figure