SceneKit features introduced in iOS 9 / Xcode 7

Let's go back to transitions and animations. As of iOS 9, we can change a character or other 3D object's blend mode very easily in SceneKit.

SceneKit features introduced in iOS 9 / Xcode 7

A display of the various blend modes in SceneKit from WWDC2015

Blend modes can be changed simply with one line, aSCNMaterial.blendMode = .Add, where aSCNMaterial is an object representing the material of SCNNode. Changing blend modes can create a number of effects. Some games use a player's ghost to show a past run through they are trying to beat, or there's the fade effect boss characters make when defeated. Combine with SCNTransaction to have characters fade in and out of these modes.

Audio nodes and 3D sound

As of iOS 9, we can place 3D sounds into our SceneKit scenes. The addAudioPlayer() function of the SCNNode class function lets us append a sound to that node, and wherever that node is in 3D space, the sound will adhere to 3D audio mixing; that is, if the audio source's positional property is set to true. Here's how we'd create 3D sound with audio nodes:

let source = SCNAudioSource(named: "sound.caf")
let soundEffect = SCNAudioPlayer(source: source)
node.addAudioPlayer(soundEffect)
source.positional = true 
source.loops = false

This gives a sound effect to the game object, the SCNNode named node.

To actually play the sound, we'd need to call SCNAction on it, as shown:

let action = SCNAction.playAudioSource(source, waitForCompletion: true) 
node.runAction(action)

The waitForCompletion property makes sure that the action goes as long as the sound is. This might not be the best for a character sound effect though as you might want it stopped midway (that is, the player hits the enemy, canceling their previously started chant, yell, or something to that degree).

Ambience and music

To add music and ambience, we could follow exactly the same method as adding a sound effect to a node: create an SCNAudioSource object; add that to an SCNAudioSource object; and add this to our node with addAudioPlayer. The only difference is that we'd loop the music and set it's positional property to false as follows:

source.positional = false 
source.loops = true

SpriteKit scene transitions in SceneKit

SpriteKit has some great scene transitions. We could make it look like a door is opening up or a page is turning. This could add extra character and polish to your game. Before iOS 9, we couldn't do these 2D transitions in our 3D SceneKit, but since Xcode 7 and iOS 9, we can do so in SceneKit and here's how:

aSCNView.presentScene(aScene, withTransition:aSKTransition,
incomingPointOfView:nil, completionHandler:nil)

Again, aSCNView is just a general reference to some SCNView object and when we present the scene to that view, we have the option of passing an SKTransition object for the withTransition parameter. The incomingPointofView parameter can be a reference to a camera's point of view during the transition, and the completionHandler parameter is the name of a completion block that is called after the scene transitions. For example, we could call the functions that start the count up of our last stage's score in a score scene that was transitioned to after the stage completed. We might not want to begin the counting and other functions of the new scene until we know that the scene has been 100% transitioned to or, in this case, after we know the total points from the prior scene.

Check out some more examples of SKTransition on the class reference page, maybe there's a transition that could help add to your game's design:

https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKTransition_Ref/index.html

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.170.223