Pause an SKAction in Spritekit with Swift

只谈情不闲聊 提交于 2019-12-18 15:55:26

问题


I have the following code to move an 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))

This moves the SKSpriteNode up and down forever. Is there a way that I could pause this SKAction? So that the SKSpriteNode will freeze in its current position, and then later when I decide, continue its movement?

I only want to pause the movement of this SKSpriteNode. I do not want to pause the SKScene. Just the movement of this 1 SKSpriteNode


回答1:


You should run an action with key:

 square.runAction(SKAction.repeatActionForever(moveSequence), withKey:"moving")

Then, use action's speed property to pause it:

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

        action.speed = 0       
}

or to unpause it:

action.speed = 1



回答2:


An alternative to @Whirlwind 's answer, in case you have a bunch of actions that need to be paused that are not in a group and not just the movement, is to just pause the node itself. All SKNodes have a paused property associated with it.

Ex. square.paused = true




回答3:


SCNNode.paused has been renamed to SNCNode.isPaused.
You should be able to do the following Pause animation: square.isPause=true Restart animation: square.isPaused=false



来源:https://stackoverflow.com/questions/34593193/pause-an-skaction-in-spritekit-with-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!