Game Development Reference
In-Depth Information
As you look over this code, you will see a variable named
orbNodePosition
that has
an x-coordinate matching the
playerNode
's x-coordinate and a y-coordinate that is 100
points above the
playerNode
.
After that, there is a
for
loop that adds 20
orbNode
objects centered above the player,
with each node 140 points above the previous node's
anchorPoint
. Go ahead and add
this code to the bottom of the
GameScene
's
init()
method, and then let's move on to
the second set of
orbNode
objects.
To add the second set of nodes, you will use the following, similar code:
orbNodePosition = CGPointMake(playerNode!.position.x + 50,
orbNodePosition.y)
for i in 0...19 {
var orbNode = SKSpriteNode(imageNamed: "PowerUp")
orbNodePosition.y += 140
orbNode.position = orbNodePosition
orbNode.physicsBody = SKPhysicsBody(circleOfRadius:
orbNode.size.width / 2)
orbNode.physicsBody!.dynamic = false
orbNode.physicsBody!.categoryBitMask
= CollisionCategoryPowerUpOrbs
orbNode.physicsBody!.collisionBitMask = 0
orbNode.name = "POWER_UP_ORB"
foregroundNode!.addChild(orbNode)
}
As you look over this code, you will see that the only change is the modification of the
variable
orbNodePosition
. The value of the
orbNodePosition
's x-coordinate is
increasing by 50, and everything else is the same. You could easily refactor this code into
a method and pass it the new x-coordinate, but the goal here is to see how everything
works. It will be refactored in the next chapter. Before moving on, go ahead and add this
code after the previous loop in the
GameScene
's
init()
method.
After you have all of this code added to the game scene, run the application. Your screen
will now look like
Figure 4-1
.