gpt4 book ai didi

ios - "Tap To Resume"暂停文本 SpriteKit

转载 作者:搜寻专家 更新时间:2023-10-30 22:09:00 25 4
gpt4 key购买 nike

我知道 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
}
}
}
...
}

编辑:

下面是我的终端输出的屏幕截图,其中包含我对上述代码的最新编辑: enter image description here

最佳答案

所以我在这里“破解”了我的解决方案。感谢 ABakerSmith 提出设置 self.speed = 0.0 的建议,操作暂停,我的标签将出现,但 physicsWorld 仍然积极的。所以我的解决方案是设置 self.speed = 0.0self.physicsWorld.speed = 0.0。当应用程序从非事件状态返回时,我只需重置 self.speed = 1.0self.physicsWorld.speed = 1.0。我确信对于这个困境还有其他解决方案,但由于 SpriteKit 已经处理了中断,我真正需要做的就是暂停 Action 和物理。

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)
}
...
}

关于ios - "Tap To Resume"暂停文本 SpriteKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29756777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com