微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Swift中为SKActions分配键

我希望有人能够帮我解决这个问题.我似乎找不到为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")}
您可以在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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐