gpt4 book ai didi

swift - SpriteKit SKAction 暂停延迟

转载 作者:可可西里 更新时间:2023-10-31 23:44:44 25 4
gpt4 key购买 nike

我有一个 spawn Action ,我使用 NSTimer 来创建它,但我将它更改为 SKAction 以便它会在场景暂停时停止:

let SpawnAction = SKAction.sequence([SKAction.waitForDuration(0.5), SKAction.runBlock(Spawn)])
self.runAction(SKAction.repeatActionForever(SpawnAction))

现在的问题是我认为代码的 waitForDuration 部分存在延迟,因为它不会在场景取消暂停后立即工作。

这是我用来暂停/取消暂停游戏的代码:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
let node = self.nodeAtPoint(location)
if (node.name == "Pause") {
PauseLabel.removeFromParent()
self.addChild(ResumeLabel)
self.runAction(SKAction.runBlock(self.pauseGame))
}
else if (node.name == "Resume") {
self.view!.paused = false
self.addChild(PauseLabel)
ResumeLabel.removeFromParent()
}
else {
//Other code...
}
}
}

func pauseGame() {
self.view!.paused = true
}

我希望有人告诉我正确暂停/取消暂停 SpriteKit 游戏的方法,因为我从互联网上获得的所有方法对我来说都不起作用。谢谢。

最佳答案

我不确定这是否适合您。但是在我用来暂停的按钮的选定处理程序中的 didMoveToView 函数下:

我使用了self.gameState = .Pause
以及使用的 self.physicsWorld.speed = 0

这些都会停止游戏 View ,物理世界有助于停止任何可能在您的 Sprite 上运行的 Action 。

希望对你有帮助。

关于swift - SpriteKit SKAction 暂停延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31394091/

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