Game Development Reference
In-Depth Information
A similar pattern applies to creating the omni light.
SCNLight* light = [SCNLight light];
light.type = SCNLightTypeOmni;
SCNNode* lightNode = [SCNNode node];
lightNode.light = light;
[scene.rootNode addChildNode:lightNode];
Asteroid Field Instantiation and Layout
Now you'll instantiate and lay out the asteroid field. First, let's retrieve the asteroid node to be cloned.
SCNNode *referenceNode = [referenceScene.rootNode childNodeWithName:@"asteroid" recursively:YES];
The node is cloned many times.
SCNNode* aClone = [referenceNode clone];
The method signatures above should be self-explanatory. However, one thing to note is that the clone—as
expected—does not perform a deep copy of the node attributes; it just retains them. Practically, this means that
meshes for cloned nodes will be shared, allowing better memory usage and performance, and that's especially
important for this example.
Then, it's just a matter of duplicating the node at different positions. In the following code, FRAND returns a
random number between 0 and MAX :
for (i= 0 ; i < MAX_NODES ; i++) {
SCNNode* aClone = [referenceNode clone];
aClone.position = SCNVector3Make(FRAND(MAX) - MAX/2.., FRAND(MAX) - MAX/2., -zDistance);
[rootNode addChildNode:aClone];
}
Asteroid Field Animation
To set up the animation of the cloned nodes, you will change the instantiation scheme of the asteroid fields a bit.
You will build an animation that creates one node at a time; each node will have its own animation and its “life” will be
tied to its animation. You create a method called setupAsteroidAnimationForNode that takes the clone created
as shown above. Within this method you build up an animation whose duration is four seconds.
Scene Kit is fully compatible with Core Animation and relies on its CAAnimation class to add animations to a
node. The reader is encouraged to read the Core Animation programming guide. 3
Even though not strictly needed by CAAnimation , you invoke setValue:forKey: on the animation object. This
allows you to retrieve the node later on when the delegate that indicates the end of the animation has its method
animationDidStop called. Listing 7-3 shows how to perform these tasks.
3 Apple Inc. “Core Animation Programming Guide.” http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/
Conceptual/CoreAnimation_guide/Introduction/Introduction.html .
 
Search WWH ::




Custom Search