在应用启动/退出时暂停Spritekit游戏.iOS8

【字号: 日期:2024-04-13浏览:29作者:雯心
(adsbygoogle = window.adsbygoogle || []).push({}); 如何解决在应用启动/退出时暂停Spritekit游戏.iOS8?

这是一种从后台模式返回后保持视图暂停的方法。

(有关Xcode 8的说明,请参见下文)

在情节提要中,

1)将视图的类更改为MyView

在视图控制器中,

2)用一个名为stayPaused的布尔值定义一个SKView子类

class MyView: SKView { var stayPaused = false override var paused: Bool {get { return super.paused}set { if (!stayPaused) {super.paused = newValue } stayPaused = false} } func setStayPaused() {if (super.paused) { self.stayPaused = true} }}

3)将视图定义为MyView

4)添加一个通知程序来设置stayPaused标志

class GameViewController: UIViewController { override func viewDidLoad() {super.viewDidLoad()if let scene = GameScene.unarchiveFromFile('GameScene') as? GameScene { // Configure the view. let skView = self.view as MyView NSNotificationCenter.defaultCenter().addobserver(skView, selector:Selector('setStayPaused'), name: 'stayPausednotification', object: nil)

在应用程序委托中,

5)发布通知以设置应用程序处于活动状态时的停留暂停标志

func applicationDidBecomeActive(application: UIApplication) { NSNotificationCenter.defaultCenter().postNotificationName('stayPausednotification', object:nil)}

在情节提要中,

1)将视图的类别从更改SKView为MyView

在视图控制器中,

2)定义一个SKView带有名为stayPaused的布尔值的子类

class MyView: SKView { var stayPaused = false override var isPaused: Bool {get { return super.isPaused}set { if (!stayPaused) {super.isPaused = newValue } stayPaused = false} } func setStayPaused() {if (super.isPaused) { self.stayPaused = true} }}

3)将视图定义为MyView

4)添加一个通知程序来设置stayPaused标志

class GameViewController: UIViewController { override func viewDidLoad() {super.viewDidLoad()if let view = self.view as! MyView? { NotificationCenter.default.addobserver(view, selector:#selector(MyView.setStayPaused), name: NSNotification.Name(rawValue: 'stayPausednotification'), object: nil)

在应用程序委托中,

5)发布通知以设置应用程序处于活动状态时的停留暂停标志

func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was prevIoUsly in the background, optionally refresh the user interface. NotificationCenter.default.post(name: NSNotification.Name(rawValue: 'stayPausednotification'), object: nil)}解决方法

我已经阅读了有关该主题的所有内容,但仍然无法弄清我的问题。我尝试在appdelegate的每个区域暂停游戏

func applicationWillResignActive(application: UIApplication!) { NSNotificationCenter.defaultCenter().postNotificationName('pauseGameScene',object: self)}func applicationDidEnterBackground(application: UIApplication!) { NSNotificationCenter.defaultCenter().postNotificationName('pauseGameScene',object: self)}func applicationWillEnterForeground(application: UIApplication!) { NSNotificationCenter.defaultCenter().postNotificationName('pauseGameScene',object: self)}func applicationDidBecomeActive(application: UIApplication!) { NSNotificationCenter.defaultCenter().postNotificationName('pauseGameScene',object: self)}

在我的控制器中:

override func viewDidLoad() { NSNotificationCenter.defaultCenter().addObserver(self,selector: 'pauseGame:',name: 'pauseGameScene',object: nil)}func pauseGame(){ self.skView.paused = true self.skView.scene!.paused = true}

我知道pauseGame可以用,因为如果我在场景中用按钮切换它,它将停止游戏。即使我在将skview和场景加载到控制器中后立即暂停它们,游戏也不会在启动时暂停。在游戏中暂停游戏很容易。但是由于某种原因,每当我退出并恢复该应用程序时,游戏便会自行取消暂停。

我注意到如果我受到黑客攻击并使用某种延迟。.我可以使它工作。但是显然这是非常愚蠢的。我只需要知道游戏在哪里暂停即可!

func delay(delay:Double,closure:()->()) { dispatch_after(dispatch_time( DISPATCH_TIME_NOW,Int64(delay * Double(NSEC_PER_SEC))),dispatch_get_main_queue(),closure)}func pauseGame(sender: UIButton!){ delay(2) {println('blah')self.skView.paused = trueself.skView.scene!.paused = true }}

相关文章: