問題是,即使場景正在運行,代碼仍然運行。是的,你正在暫停場景,是的,視覺效果確實可以運行......但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的相同位置來修復它。 –
完美,很高興它幫助你弄明白了 –