Adding custom game logic to Scene Kit (Swift)

不羁岁月 提交于 2019-12-22 00:51:02

问题


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!


回答1:


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 ...
}


来源:https://stackoverflow.com/questions/27847828/adding-custom-game-logic-to-scene-kit-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!