如何在Swift中为SKActions分配键 [英] How to assign Keys to SKActions in Swift

查看:192
本文介绍了如何在Swift中为SKActions分配键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was hoping someone would be able to help me with this problem. I can't seem to find a way to assign a key to an SKAction for Sprite Kit for the removeActionWithKey method. I have also tried assinging the action to a key in a dictionary but the program didn't recognize the key assignment and thus returned a nil value.

这是我尝试做的事情:

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方法中完成

You can do it in the runAction method

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

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

This will allow you to remove the action by name

sprite.removeActionForKey("Cool Action")

仅凭经验,我建议将操作字符串名称放在变量中.它将减少动作名称拼写错误的奇怪错误.

Just from experience, I'd recommend placing action string names in variables. It will cut down on the weird bugs from very slightly misspelled action names.

因此,此版本的改进版本是var

So an improved version of this is a class var

let coolActionName = "Cool Action"

// Your other code

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

// Time to remove the action
sprite.removeActionForKey(coolActionName)

这篇关于如何在Swift中为SKActions分配键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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