Game Development Reference
In-Depth Information
Make this change to the
didBeginContact()
method and run the app again. When
you play this time, try to make the player come into contact with the new black hole.
When you do, you will notice the player quits responding to taps and contact with other
nodes and then slowly falls until it falls through the bottom of the scene. At this point, the
game is essentially over.
All right, it is finally time to use some
SKAction
s. If you remember from the previous
section, there are two steps involved when using an
SKAction
. First you create the ac-
tion you would like to use, and second you tell the node that you want to apply the action
to run the action. Let's begin with the action sequence used earlier in this chapter.
let moveLeftAction = SKAction.moveToX(0.0, duration: 2.0)
let moveRightAction = SKAction.moveToX(size.width,
duration: 2.0)
let actionSequence = SKAction.sequence([moveLeftAction,
moveRightAction])
let moveAction
= SKAction.repeatActionForever(actionSequence)
As you look at this code, you will recognize it from earlier in the chapter when I first in-
troduced
SKAction
s. The first line creates an action that will move the node that runs it
to point 0.0 on the x-axis, and the second line moves the node back to the far-right side of
the scene. After this, both of these actions are used to create a sequence of actions, and
this new action is stored in the variable
actionSequence
. Finally, the
ac-
tionSequence
is used to create a repeating action using the
SKAc-
tion.repeatActionForever()
class method. After you have looked this code
over a bit, copy it to the top of the
addBlackHolesToForeGround()
method.
The final step to make the black hole move back and forth across the scene is to tell the
blackHoleNode
to actually run the action. This is done using the following single line:
blackHoleNode.runAction(moveAction)
Add this line to the end of the
addBlackHolesToForeground()
method and run
the app again. This time you will see the black hole moving back and forth across the
scene for as long as the game is being run.
There is one more thing I want to do with the
addBlackHolesToForeground()
method before moving on. You may have noticed the name of the
addBlack-
HolesToForeground()
method mentions more than one black hole. This was inten-