jctwood
jctwood

Reputation: 131

Adding custom game logic to Scene Kit (Swift)

I need to add a game loop to my GameViewController (From the Swift "Game" template for iOS development) in order to create an application and found this reference page explaining how to do this:

https://developer.apple.com/library/prerelease/ios/documentation/SceneKit/Reference/SCNSceneRendererDelegate_Protocol/

However when I try to set the delegate of the SCNView to be the ViewController it throws up warnings and errors (inside viewDidLoad()):

gameView.delegate = self

Where gameView is connected to an SCNView in my storyboard:

@IBOutlet weak var gameView: SCNView!

It would be brilliant if someone could link a code example of setting up game logic using Swift and Scene Kit or explain it to me from the ground up. Thank you!

Upvotes: 1

Views: 1187

Answers (1)

rickster
rickster

Reputation: 126167

When you assign:

gameView.delegate = self

This requires that self be a class that declares conformance to the SCNSceneRendererDelegate protocol. To make your view controller class declare protocol conformance, use the syntax described in the Swift book:

class ViewController: UIViewController, SCNSceneRendererDelegate  {
    //                ~~~~~~~~~~~~~~~~  ~~~~~~~~~~~~~~~~~~~~~~~~ ^
    //                ^- superclass     ^- protocol              |
    //                   more protocols if you conform to them --/ 

    // ... rest of class definition ...
}

Upvotes: 4

Related Questions