【问题标题】:How to assign Keys to SKActions in Swift如何在 Swift 中为 SKAction 分配键
【发布时间】:2014-09-07 08:43:13
【问题描述】:

我希望有人能够帮助我解决这个问题。对于 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")}

【问题讨论】:

    标签: key swift sprite-kit skaction


    【解决方案1】:

    你可以在runAction方法中做到这一点

    sprite.runAction(myCoolAction, withKey: "Cool Action")
    

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

    sprite.removeActionForKey("Cool Action")
    

    根据经验,我建议将操作字符串名称放在变量中。它将减少由于动作名称拼写错误而引起的奇怪错误。

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

    let coolActionName = "Cool Action"
    
    // Your other code
    
    // Run the action
    sprite.runAction(myCoolAction, withKey: coolActionName)
    
    // Time to remove the action
    sprite.removeActionForKey(coolActionName)
    

    【讨论】:

    • 很好的答案@Jeremy。我建议在使用键时,将字符串存储为变量,以便 Xcode 自动启动它。此外,这将防止拼写错误。所以,让 keyCoolActionName = "coolActionName" 并通过 keyCoolActionName 调用它。
    猜你喜欢
    • 1970-01-01
    • 2016-03-07
    • 2018-06-03
    • 2016-04-08
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-12
    相关资源
    最近更新 更多