Could not cast value of type 'UIView' (0x112484eb0) to 'SKView' (0x111646718)

偶尔善良 提交于 2019-11-28 11:53:27

Go to your Storyboard, select your UIViewController that contains the SpriteKit game, and select the view from left menu:

Now go to Identity Inspector and make sure Class is SKView and not UIView:

You should now be able to compile this part of code from your UIViewController:

// Configure the view.
SKView * skView = (SKView *)self.view;

Or in Swift:

let skView = self.view as! SKView

For those who are not wanting to use a Storyboard, you can simple create the view as an SKView in the ViewController's loadView function.

class ViewController: UIViewController {
  override func loadView() {
    self.view = SKView()
  }

  override func viewDidLoad() {
    let skView = view as! SKView
    ...
  }
}

Simple fix. You just have to change the View in which ever view controller your using from UIView to SKView.

I finally fixed it! Instead of putting the functions in the GameViewController and calling them from game scene, I had to put the functions in gameScene and replace view with self.view!.

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