linking GameViewController.swift to GameScene.swift

后端 未结 2 375
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-18 08:45

I have created a UI elements on main.storyboard which i require to be hidden until the game is over and the once the player tap the screen to dismiss. Main.storyboard is lin

2条回答
  •  别那么骄傲
    2020-12-18 09:11

    create reference of GameViewController in GameScene class like this way

    class GameScene: SKScene, SKPhysicsContactDelegate {
    
        var referenceOfGameViewController : GameViewController!
    
    }
    

    in GameViewController pass the reference like this way

       class GameViewController: UIViewController {
    
           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
                scene.scaleMode = .aspectFill
                scene.referenceOfGameViewController = self
                // Present the scene
                view.presentScene(scene)
            }
    
    
        view.ignoresSiblingOrder = true
    
        view.showsFPS = true
        view.showsNodeCount = true
        }
      }
    }
    

    by using this line you can pass reference to GameScene class

            scene.referenceOfGameViewController = self
    

    Now In GameScene class you can access all the variable of GameViewController like this way

    referenceOfGameViewController.fbButton.hidden = false
    referenceOfGameViewController.gameOverPopUP.hidden = false
    

提交回复
热议问题