Game Development Reference
In-Depth Information
Scene Kit also allows you to mix some OpenGL code into your scenes; however, I will
not address any of these items since they are more advanced topics.
Starting Animations
You'll start with the
Obstacle
class; for each obstacle, you will do something slightly
different. To keep things simple, you will use CoreAnimation's
CABasicAnimation
class to rotate these first few obstacles.
PyramidNode
In the following code, you rotate the
PyramidNode
around the x-axis. When you do this
animation, the pyramid node will rotate below the floor from the base of the pyramid.
let rotation = CABasicAnimation(keyPath: "rotation")
rotation.fromValue = NSValue(SCNVector4:SCNVector4Make(0,
0, 0, 0))
rotation.toValue = NSValue(SCNVector4:SCNVector4Make(1, 0,
0, Float(2.0*M_PI)))
rotation.duration = 5
rotation.repeatCount = .infinity
pyramidNode.addAnimation(rotation, forKey: "rotation")
In this code, you are doing a few things. First you use the
CABasicAnimation
class to
do basic animations.
The
.fromValue
property is where you define a starting vector or current
location of the object. For this animation, you simply set everything to 0 as
the starting point.
The
.toValue
property is where you want the object to end up when the
animation is complete.
The
.duration
property is how long in seconds it takes to complete the
animation. You can adjust this as you see fit.