How to call method from ViewController in GameScene

后端 未结 4 875
遇见更好的自我
遇见更好的自我 2020-12-01 13:21

I have a method that has a custom segue in my viewController that looks like this:

func gameOver() {
    performSegueWithIdentifier(\"GameOver\", sender: nil         


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-01 14:04

    the reason this doesnt work is that you are creating a NEW instance of GameViewController and then you're calling gameOver on that. What you really want to do is reference your existing GameViewController

    theres a few ways to do this, I'll give you one example.

    add a viewController property to your GameScene class

    class GameScene {
    
        // we need to make sure to set this when we create our GameScene
        var viewController: GameViewController!
    

    in your GameViewController file

    // after GameScene is instantiated
    gameScene.viewController = self
    

    now we have a reference to viewController, lets use it in our GameScene class

    // somewhere in GameScene
    self.viewController.gameOver()
    

提交回复
热议问题