0

I want to call a func of GameViewController from the GameScene. -> If the game Ends I want to call GameViewController().GameOver()

I tried now a lot of different things like this one: LINK (I tried every answer more than once, still not working)

But doesn't matter what I tried it doesn't even call the func.

Hope anyone can help me with this.

CODE: GameViewController:

class GameViewController: UIViewController {

    @IBOutlet weak var Button: UIButton!

    override func viewDidLoad() {
    super.viewDidLoad()
        if let view = self.view as! SKView? {
        // Load the SKScene from 'GameScene.sks'
        if let scene = GameScene(fileNamed: "GameScene") {
            // Set the scale mode to scale to fit the window
                if UIDevice().userInterfaceIdiom == .phone {
                    scene.scaleMode = .aspectFill
                }else{
                    scene.scaleMode = .aspectFit
                }


            // Present the scene
            view.presentScene(scene)

            skView = view



        }

        view.ignoresSiblingOrder = true

        view.showsFPS = false
        view.showsNodeCount = false


    }

     ... 
    @IBAction func Button(_ sender: Any) {

    animation()



         if let gameScene = skView.scene as? GameScene { // check to see if the current scene is the game scene
             gameScene.start()
         }
    }

func animation(){
    UIButton.animate(withDuration: 1, animations: {
        self.Button?.alpha = 0
    })
}

    func GameOver(){
        UIButton.animate(withDuration: 1, animations: {
            self.Button?.alpha = 1
        })
    }
}

GameScene:

class GameScene: SKScene, SKPhysicsContactDelegate {

   ...

   func torpedoDidCollideWithAlien (torpedoNode:SKSpriteNode, alienNode:SKSpriteNode) {

     GameViewController().GameOver()
     removeAllActions()
     removeAllChildren()

  }
}

1 Answers1

0

Option 1:

You should pass GameViewController instance when you are calling GameScene from GameViewController at the first time, Then just save this instance in some var gameVC: GameViewController! and initialize it like you do in prepare for segue method.

Then you'll be able to call gameVC.GameOver()

Option 2:

Put this in GameScene when you want to call GameOver():

if let controller = self.view?.window?.rootViewController as? GameViewController {
   controller.GameOver()
}
Community
  • 1
  • 1
Ido
  • 473
  • 4
  • 16
  • Isn't this the same as the first answer of the link? Because I don't know why, but this solution isn't working at all? –  Jul 18 '18 at 11:45
  • Oh yes, I didn't saw this link... can you please edit you'r question with the end point of the `GameViewController` and the enter point to the `GameScene` so it will be more informative? – Ido Jul 18 '18 at 11:50
  • the end point of the game view controller? you mean when I start the game scene its like usual in the view did load. So the game starts right away and at the end of the game scene when the two objects collide I just delete all node's and than a button appears from the game view controller. sorry if I understood it wrong my English isn't that good –  Jul 18 '18 at 12:44
  • I mean when you are calling `GameScene` in `GameViewController`, like perform segue. – Ido Jul 18 '18 at 13:14
  • yes like I said I call game scene in the view did load –  Jul 18 '18 at 13:25
  • Can you add the `viewDidLoad()` code so it will be easier to me to understand what you can try to do? – Ido Jul 18 '18 at 13:27
  • So I added the important stuff. Any idea how it could work or why your solution doesn't work? –  Jul 18 '18 at 14:56
  • Actually if I put this in GameViewController: scene.viewController = self, I'm getting a error that the scene don't got any member named view controller –  Jul 18 '18 at 16:15
  • My mistake, don't include this line. – Ido Jul 18 '18 at 20:53