如何在Swift中为SKActions分配键

use*_*167 5 key sprite-kit skaction swift

我希望有人能够帮我解决这个问题.我似乎找不到为removeActionWithKey方法为Sprite Kit分配SKAction的方法.我还尝试将操作分配给字典中的键,但程序无法识别键分配,因此返回了nil值.

这是我试图做的:

var textureanimation = SKAction.repeatActionForever(SKAction.animateWithTextures(_walkingframes, timePerFrame: 0.1))
var dictionary = ["animation": textureanimation]
    object.runAction(actionForKey("animation"))

    var sequence = [SKAction.moveTo(tap_position, duration: time_duration),
        SKAction.runBlock{

            object.removeActionForKey("animation")}
Run Code Online (Sandbox Code Playgroud)

Jer*_*emy 14

您可以在runAction方法中执行此操作

sprite.runAction(myCoolAction, withKey: "Cool Action")
Run Code Online (Sandbox Code Playgroud)

这将允许您按名称删除操作

sprite.removeActionForKey("Cool Action")
Run Code Online (Sandbox Code Playgroud)

根据经验,我建议在变量中放置动作字符串名称.它将减少来自非常轻微拼写错误的动作名称的奇怪错误.

所以这个改进版本是一个类var

let coolActionName = "Cool Action"

// Your other code

// Run the action
sprite.runAction(myCoolAction, withKey: coolActionName)

// Time to remove the action
sprite.removeActionForKey(coolActionName)
Run Code Online (Sandbox Code Playgroud)