简体   繁体   English

如何将applicationWillTerminate用于特定的SKScene?

[英]How to use applicationWillTerminate for a specific SKScene?

I have an SKScene that displays the players that joined the current room. 我有一个SKScene ,显示加入当前房间的玩家。 If any of those players leaves the room (by clicking on the Leave button) their players list will be updated. 如果这些玩家中的任何一个离开房间(通过点击“离开”按钮),他们的玩家列表将被更新。

But if I close the app from one of the players, that specific player remains in the room. 但是如果我从其中一个玩家关闭应用程序,那个特定的玩家仍然留在房间里。 I want to call my leaveRoom function from the applicationWillTerminate so all the data will work fine. 我想从applicationWillTerminate调用我的leaveRoom函数,这样所有数据都可以正常工作。 Is it possible? 可能吗? How can I resolve this issue? 我该如何解决这个问题?

You can make an observer to intercept it: 你可以让观察者拦截它:

override func didMove(to view: SKView) {        
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(GameScene.applicationWillTerminate(notification:)),
            name: NSNotification.Name.UIApplicationWillTerminate,
            object: nil)
}
func applicationWillTerminate(notification: NSNotification) {
   // put your code here
}

You can remove the observer to : 您可以删除观察者:

override func willMove(from view: SKView) {
    NotificationCenter.default.removeObserver(self)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM