Game Development Reference
In-Depth Information
Now you have some obstacles that are moving around and looking good in the scene.
When you start the game, look around—you will see all the objects animating. If you are
running the game in the simulator, you will notice that it is performing terribly. However,
if you run the game on a device, you'll see it is animating at a minimum of 35 frames per
second, if not closer to 60, depending on which device you are running the game on. I
suggest running it in the simulator if you haven't so that you can see how the hardware af-
fects the frame rate.
More Animations
Like with Sprite Kit, you can use actions to animate your Scene Kit nodes. Next you will
add some of the actions to the nodes. This way, you will have some CoreAnimation ac-
tions as well as the SCNAction taking place at the same time.
Using the SCNAction on a node will allow you to change the structure and content of
the node. These actions are animated over more than one frame of animation rendered by
the scene.
For your first type of action, go to the Obstacle class. Here you will make the globe
bounce up and down as it rotates around its axis. Find the function where you created the
globe, and do the following before returning the node:
let moveGlobeUp = SCNAction.moveByX(0.0, y: 10.0, z: 0.0,
duration: 1.0)
let moveGlobeDown = SCNAction.moveByX(0.0, y: -10.0, z:
0.0, duration: 1.0)
let sequence = SCNAction.sequence([moveGlobeUp,
moveGlobeDown])
let repeateSequence
= SCNAction.repeatActionForever(sequence)
globeNode.runAction(repeateSequence)
In this code segment, you have animated the position of the globeNode . First you cre-
ated a new SCNAction using the constructor moveByX:y:z:duration , which
moves a node to a new position. After this, you created another SCNAction that moves
the globeNode back to the original position. In this action, you simply reversed the y-
axis the same amount as you moved it.
Search WWH ::




Custom Search