Mik*_*ike 6 ios sprite-kit swift
我知道当应用程序进入非活动状态时,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)
...
}
Run Code Online (Sandbox Code Playgroud)
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
}
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
编辑:
下面是我的终端输出的屏幕截图,其中包含我对上述代码的最新修改:

所以我在这里“破解”了我的解决方案。由于ABakerSmith设置的建议self.speed = 0.0,操作已暂停,我的标签将出现,但physicsWorld仍处于活动状态。所以我的解决方案是设置self.speed = 0.0AND 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
}
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
@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)
}
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2405 次 |
| 最近记录: |