问题是,即使场景正在运行,代码仍然运行。是的,你正在暂停场景,是的,视觉效果确实可以运行......但isPaused线后面的视觉效果也可以运行并重置刚刚更改的视觉效果。
这里是一个非常简单的例子,这是如何工作的。现场有3个盒子;底部框具有反复放大和缩小的动作,并且在场景暂停时停止。顶盒会在按下时暂停游戏,并显示将取消暂停游戏的中间框。
class GameScene: SKScene {
private var test: SKSpriteNode!
private var test2: SKSpriteNode!
private var test3: SKSpriteNode!
override func didMove(to view: SKView) {
backgroundColor = .clear
test = SKSpriteNode(color: .blue, size: CGSize(width: 200, height: 200))
test.position = CGPoint(x: 0, y: 350)
addChild(test)
test2 = SKSpriteNode(color: .red, size: CGSize(width: 200, height: 200))
test2.position = CGPoint(x: 0, y: 0)
test2.alpha = 0
addChild(test2)
test3 = SKSpriteNode(color: .yellow, size: CGSize(width: 200, height: 200))
test3.position = CGPoint(x: 0, y: -350)
addChild(test3)
let scaleDown = SKAction.scale(to: 0.1, duration: 1.0)
let scaleUp = SKAction.scale(to: 1.0, duration: 1.0)
let sequence = SKAction.sequence([scaleDown, scaleUp])
let repeater = SKAction.repeatForever(sequence)
test3.run(repeater)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first as UITouch! {
let touchLocation = touch.location(in: self)
if test.contains(touchLocation) {
print("pausing")
self.test2.alpha = 1
self.test.alpha = 0
self.isPaused = true
}
if test2.contains(touchLocation) {
print("unpausing")
self.test2.alpha = 0
self.test.alpha = 1
self.isPaused = false
}
}
}
}
这很有道理。谢谢。我完全没有想到会发生这种情况,因为行动和图像都停顿了。我想我会通过将恢复代码放在isPaused = false的相同位置来修复它。 –
完美,很高兴它帮助你弄明白了 –