“点按恢复”暂停文本SpriteKit

我知道SpriteKit已经处理暂停游戏,当应用程序进入不活动状态,但我想要做的是添加一个SKLabelNode“点击恢复”,当应用程序重新进入活动状态。 现在它正确地调用我的function,并暂停游戏,但文本不显示。

AppDelegate.swift

func applicationWillResignActive(application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. println("applicationWillResignActive") NSNotificationCenter.defaultCenter().postNotificationName("PauseGameScene", object: self) NSNotificationCenter.defaultCenter().postNotificationName("ShowPauseText", object: self) ... } 

GameScene.swift

 class GameScene: SKScene, SKPhysicsContactDelegate { ... let tapToResume = SKLabelNode(fontNamed: "Noteworthy") ... override func didMoveToView(view: SKView) { ... NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("pauseGameScene"), name: "PauseGameScene", object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("showPauseText"), name: "ShowPauseText", object: nil) tapToResume.text = "tap to resume" tapToResume.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame)) tapToResume.fontSize = 55 tapToResume.hidden = true self.addChild(tapToResume) ... } func pauseGameScene() { println("pause game") self.view?.paused = true } func showPauseText() { if self.view?.paused == true { tapToResume.hidden = false println("show text") } } override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { ... if self.paused { self.view?.paused = false if tapToResume.hidden == false { tapToResume.hidden = true } } } ... } 

编辑:

下面是我的terminal输出的截图,我最近编辑了我的上面的代码: 在这里输入图像说明

所以我在这里“黑了”我的解决scheme。 感谢ABakerSmithbuild议设置self.speed = 0.0 ,动作已经暂停,我的标签将出现,但physicsWorld仍然是活动的。 所以我的解决scheme是设置self.speed = 0.0self.physicsWorld.speed = 0.0 。 当应用程序从非活动状态返回时,我只是重置self.speed = 1.0self.physicsWorld.speed = 1.0 。 我相信还有其他的解决方法,但是由于SpriteKit已经处理了中断,我真正需要做的就是暂停动作和物理。

GameScene.swift

 class GameScene: SKScene, SKPhysicsContactDelegate { let tapToResume = SKLabelNode(fontNamed: "Noteworthy") ... override func didMoveToView(view: SKView) { ... NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("pauseGameScene"), name: "PauseGameScene", object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("showPauseText"), name: "ShowPauseText", object: nil) } func pauseGameScene() { self.physicsWorld.speed = 0.0 self.speed = 0.0 } func showPauseText() { if self.physicsWorld.speed == 0.0 { tapToResume.hidden = false } } override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { ... if self.physicsWorld.speed == 0.0 { self.physicsWorld.speed = 1.0 self.speed = 1.0 if tapToResume.hidden == false { tapToResume.hidden = true } } } ... } 

AppDelegate.swift

 @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { func applicationWillResignActive(application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. NSNotificationCenter.defaultCenter().postNotificationName("PauseGameScene", object: self) NSNotificationCenter.defaultCenter().postNotificationName("ShowPauseText", object: self) } ... } 

我相信你的问题是设置self.view?.paused = true ,正如@Steve在评论和@Linus G.在他的回答中指出的那样。 因此,当您试图取消隐藏标签时,由于视图已暂停,因此没有任何事情发生。

我试着用self.paused来暂停SKScene 。 这解决了显示标签的问题,但实际上并没有暂停现场。 这可能是由于:自从iOS8以后,SpriteKit在游戏进入后台时自动暂停游戏,并在游戏进入前台时暂停游戏。 因此,试图设置self.paused = true ,使用applicationWillResignActive ,没有效果,因为它在进入前景时未被暂停。

要解决这个问题,你可以观察UIApplicationWillResignActiveNotification 。 然后,当应用程序将要退出时,您将设置self.speed = 0 ,这与暂停SKScene具有相同的效果。 这将显示标签并根据需要暂停场景。

例如:

 class GameScene: SKScene { let tapToResume = SKLabelNode(fontNamed: "Noteworthy") override func didMoveToView(view: SKView) { NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("pauseScene"), name: UIApplicationWillResignActiveNotification, object: nil) tapToResume.text = "tap to resume" tapToResume.position = CGPoint(x: frame.midX, y: frame.midY) tapToResume.fontSize = 55 tapToResume.hidden = true self.addChild(tapToResume) } func pauseScene() { self.speed = 0.0 tapToResume.hidden = false } override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { // Check the label was pressed here. if labelWasPressed { self.speed = 1.0 tapToResume.hidden = true } } 

我想我得到了这个问题。 你应该先调用pauseGameScene() 。 然后view?.pausedtrue然后你可以调用showPauseText()

希望帮助:)