Matthew Anguelo Matthew Anguelo - 1 year ago 96
Swift Question

SceneKit Renderer Function Not Working Swift?

I am currently working with Swift 3 and SceneKit. My issue seems to be that my renderer function is not working properly. I don't know what I am doing wrong. The program runs but none of the code in the:

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval)
seems to execute.

Here is my code up to the renderer function.

import UIKit
import QuartzCore
import SceneKit

class GameViewController: UIViewController, SCNSceneRendererDelegate

let scene = SCNScene()
let cameraNode = SCNNode()
var person = SCNNode()

let firstBox = SCNNode()

var goingLeft = Bool()

var tempBox = SCNNode()

var prevBoxNumber = Int()

var boxNumber = Int()

override func viewDidLoad() {

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {

let deleteBox = self.scene.rootNode.childNode(withName: "\(prevBoxNumber)", recursively: true)

if (deleteBox?.position.x)! > person.position.x + 1 || (deleteBox?.position.z)! > person.position.z + 1{

prevBoxNumber += 1



Here is my creatBox() function:

func createBox(){
tempBox = SCNNode(geometry: firstBox.geometry)
let previousBox = scene.rootNode.childNode(withName: "\(boxNumber)", recursively: true)
boxNumber += 1 = "\(boxNumber)"

let randomNumber = arc4random() % 2

switch randomNumber {
case 0:
tempBox.position = SCNVector3Make((previousBox?.position.x)! - firstBox.scale.x, (previousBox?.position.y)! , (previousBox?.position.z)!)
case 1:
tempBox.position = SCNVector3Make((previousBox?.position.x)! , (previousBox?.position.y)! , (previousBox?.position.z)! - firstBox.scale.z)


here is my createScene()

self.view.backgroundColor = UIColor.white
let sceneView = self.view as! SCNView
boxNumber = 0
prevBoxNumber = 0
sceneView.delegate? = self
sceneView.scene = scene
//Create Box
let firstBoxGeo = SCNBox(width: 1, height: 1.5, length: 1, chamferRadius: 0)
firstBox.geometry = firstBoxGeo
let boxMaterial = SCNMaterial()
boxMaterial.diffuse.contents = UIColor(red: 0.2, green: 0.8, blue: 0.9, alpha: 1.0)
firstBoxGeo.materials = [boxMaterial]
firstBox.position = SCNVector3Make(0, 0, 0)
scene.rootNode.addChildNode(firstBox) = "\(boxNumber)"
for _ in 0...6 {

Please let me know if there is anything I am missing or doing wrong. Thanks!

Answer Source

I haven't seen your createScene() method so I'm guessing here :)

Have you added your GameViewController as a delegate to your SCNView instance?

When you have created your SCNView, maybe like this:

let scnView = self.view as! SCNView

you need to add yourself as delegate, like so:

scnView.delegate = self

And then you should see your

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval)

being executed.

Edit (after having seen your createScene() code)

Try changing:

sceneView.delegate? = self


sceneView.delegate = self

(omit the ?)

if I do so I can see that your renderer delegate method is invoked twice at least. To make it continue being invoked you can add:

sceneView.isPlaying = true 

somewhere in your createScene() method if that is what you are aiming for.

Hope that helps you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download