Game Development Reference
In-Depth Information
For your first obstacle, create a pyramid ( Listing 11-1 ) .
Listing 11-1 . PyramidNode
class func PyramidNode() -> SCNNode {
let pyramid = SCNPyramid(width: 10.0, height: 20.0,
length: 10.0)
let pyramidNode = SCNNode(geometry: pyramid)
pyramidNode.name = "pyramid"
let position = SCNVector3Make(30, 0, -40)
pyramidNode.position = position
pyramidNode.geometry?.firstMaterial?.diffuse.contents
= UIColor.blueColor()
pyramidNode.geometry?.firstMaterial?.shininess = 1.0
return pyramidNode
}
In this bit of code, you create a geometric object using the SCNPyramid Next you create
an SCNNode , and this time you are creating it using the geometry. This allows you to eas-
ily manipulate the pyramid as you would any SCNNode . As with any node, you need to
give it a position. Don't worry too much about the pyramidNode.geo-
metry?.firstMaterial?.diffuse.contents = UIColor.blueColor()
part because you will learn more about materials in the next chapter. At this time, you are
just giving the pyramid some color so that you will be able to see it.
Now that you have the code to create your new pyramid obstacle, you can go back to the
GameViewController.swift file and add the code so that when you run the game,
the pyramid node is added to your scene. In the viewDidLoad() method, you add the
pyramid to the scene's rootNode as a child node.
// Create the scene
let mainScene = createMainScene()
mainScene.rootNode.addChildNode(createFloorNode())
mainScene.rootNode.addChildNode(Obstacles.PyramidNode())
// Get the games main view
let sceneView = self.view as SCNView
sceneView.scene = scene
Search WWH ::




Custom Search