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

前端 未结 4 698
春和景丽
春和景丽 2020-12-10 01:37

Could not cast value of type \'UIView\' (0x112484eb0) to \'SKView\' (0x111646718). I keep on getting this error. Can anyone help me because the line of code is there by defa

相关标签:
4条回答
  • 2020-12-10 01:47

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

    0 讨论(0)
  • 2020-12-10 01:58

    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
        ...
      }
    }
    
    0 讨论(0)
  • 2020-12-10 01:58

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

    0 讨论(0)
  • 2020-12-10 02:03

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

    enter image description here

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

    enter image description here

    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
    
    0 讨论(0)
提交回复
热议问题