使用 Swift 在 Spritekit 中暂停 SKAction [英] Pause an SKAction in Spritekit with Swift

查看:22
本文介绍了使用 Swift 在 Spritekit 中暂停 SKAction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来移动 SKSpriteNode.

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))

这将永远上下移动 SKSpriteNode.有没有办法可以暂停这个 SKAction?这样 SKSpriteNode 将冻结在其当前位置,然后当我决定之后,继续其移动?

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?

我只想暂停这个 SKSpriteNode 的移动.我不想暂停 SKScene.只是这1个SKSpriteNode

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

推荐答案

你应该用 key 运行一个动作:

You should run an action with key:

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

然后,使用动作的 speed 属性来暂停它:

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

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

        action.speed = 0       
}

或取消暂停:

action.speed = 1

这篇关于使用 Swift 在 Spritekit 中暂停 SKAction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆