使用Swift在Spritekit中暂停SKAction

Que*_*ons 9 sprite-kit skaction swift

我有以下代码来移动SKSpriteNode.

let moveDown = SKAction.moveBy(CGVectorMake(0, -120), duration: 1)
let moveUp = SKAction.moveBy(CGVectorMake(0, +120), duration: 1)
let moveSequence = SKAction.sequence([moveDown, moveUp])
square.runAction(SKAction.repeatActionForever(moveSequence))
Run Code Online (Sandbox Code Playgroud)

这会SKSpriteNode永远地上下移动.有没有办法可以暂停这个SKAction?那么SKSpriteNode意志会冻结在当前的位置,然后在我决定时,继续它的运动?

我只想暂停这一动作SKSpriteNode.我不想暂停SKScene.只是这个运动1SKSpriteNode

Whi*_*ind 11

您应该使用键运行操作:

 square.runAction(SKAction.repeatActionForever(moveSequence), withKey:"moving")
Run Code Online (Sandbox Code Playgroud)

然后,使用action的speed属性暂停它:

if let action = square.actionForKey("moving") {

        action.speed = 0       
}
Run Code Online (Sandbox Code Playgroud)

或者取消暂停:

action.speed = 1
Run Code Online (Sandbox Code Playgroud)


Kni*_*gon 8

@Whirlwind的答案的替代方案,如果你有一堆需要暂停但不在一个组而不仅仅是移动的动作,就是暂停节点本身.所有SKNode都有paused与之关联的属性.

防爆. square.paused = true