我知道当应用程序进入非活动状态时,SpriteKit已处理暂停游戏,但我尝试做的是添加SKLabelNode"点击以恢复"当应用程序重新进入活动状态时。现在它正确调用我的功能并暂停游戏,但文本没有显示。
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)
...
}
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
}
}
}
...
}
编辑:
下面是我的终端输出的屏幕截图,其中包含我对上述代码的最新修改:
答案 0 :(得分:4)
所以我&#34;黑客攻击&#34;我的解决方案。感谢ABakerSmith
建议设置self.speed = 0.0
,操作已暂停,我的标签会显示,但physicsWorld
仍然有效。所以我的解决方案是设置self.speed = 0.0
和self.physicsWorld.speed = 0.0
。当应用从非活动状态返回时,我只需重置self.speed = 1.0
和self.physicsWorld.speed = 1.0
。我确信还有其他解决方案可以应对这种困境,但是由于SpriteKit已经处理了中断,我真正需要做的就是暂停动作和物理。
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
}
}
}
...
}
@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)
}
...
}
答案 1 :(得分:3)
我相信你的问题是设置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
}
}
答案 2 :(得分:2)
我想我遇到了问题。您应该先致电pauseGameScene()
。然后view?.paused
为true
。 然后您可以致电showPauseText()
。
希望有所帮助:)