Call the GameScene method from AppDelegate (Swift 3, SpriteKit, Xcode 8)

I use Spritekit and swift 3 to create a game, my problem is that I am trying to call a method pauseGame()present in the GameScene class (subclass of SKScene) from the AppDelegate file in the method applicationWillResignActive(_ application: UIApplication).

I'm already trying to instantiate the GameScene class and then call the method in my AppDelegate file this way, although compiler errors do not work:

func applicationWillResignActive(_ application: UIApplication) {

    if let gameScene = GameScene(fileNamed: "GameScene") {

        gameScene.pauseGame()
    }
}

How can i solve this? Thanks in advance.

+1
source share
1 answer

A new instance of your GameScene is created. To pause an existing instance, you need to add a link to it in AppDelegate.

GameScene , . AppDelegate.

GameScene viewDidLoad():

let app = UIApplication.shared

//Register for the applicationWillResignActive anywhere in your app.
NotificationCenter.default.addObserver(self, selector: #selector(GameScene.applicationWillResignActive(notification:)), name: NSNotification.Name.UIApplicationWillResignActive, object: app)

GameScene, :

func applicationWillResignActive(notification: NSNotification) {
     pauseGame()
}
0

Source: https://habr.com/ru/post/1681869/


All Articles