【发布时间】:2015-04-20 19:33:45
【问题描述】:
我知道 SpriteKit 已经在应用程序进入非活动状态时处理暂停游戏,但我想做的是在应用程序重新进入活动状态时添加一个 SKLabelNode “点击恢复”。现在它正在正确调用我的函数并暂停游戏,但没有显示文本。
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
}
}
}
...
}
编辑:
下面是我的终端输出的屏幕截图,其中包含我对上述代码的最新编辑:
【问题讨论】:
-
您不应该设置
SKLabelNode的text属性吗?还是您只是没有在上面的代码中包含该位? -
抱歉,代码在那里,只是忘记添加了
-
您是否在暂停视图之前调用 showPauseText?您似乎是先暂停,然后再尝试取消隐藏文本。
-
我只是尝试切换它们,先调用显示文本,文本仍然没有显示出来。
-
可能是您的
fontNamed参数(当您声明标签时)传递给它的字体名称不正确,或者标签可能隐藏在另一个精灵后面,因为它的 z-index 较低?
标签: ios swift sprite-kit