检查SKAction是否包含操作(swift)

检查SKAction是否包含操作(swift),swift,sprite-kit,skaction,Swift,Sprite Kit,Skaction,我正试图用swift编写一个小游戏。我正在翻译一些旧的obj-C sprite工具包代码,但找不到如何翻译这些代码。在obj-C中,SKACtion有一个名为hasActions的成员,它告诉您vars是否包含操作,但在swift中,这些属性似乎不存在,也没有执行相同操作的方法 原始obj-C代码: SKAction *animAction = [self.player actionForKey:animationKey]; // If this animation is alread

我正试图用swift编写一个小游戏。我正在翻译一些旧的obj-C sprite工具包代码,但找不到如何翻译这些代码。在obj-C中,SKACtion有一个名为hasActions的成员,它告诉您vars是否包含操作,但在swift中,这些属性似乎不存在,也没有执行相同操作的方法

原始obj-C代码:

SKAction *animAction = [self.player actionForKey:animationKey];

    // If this animation is already running or there are no frames we exit
    if ( animAction || animationFrames.count < 1 )
    {
        return;
    }
SKAction*animation=[self.player actionForKey:animationKey];
//如果此动画已在运行或没有帧,我们将退出
if(animation | | animationFrames.count<1)
{
返回;
}
Swift代码

var animAction : SKAction = player.actionForKey(animationKey)!

// If this animation is already running or there are no frames we exit
if animAction || animationFrames.count < 1
{
    return
}
var animation:SKAction=player.actionForKey(animationKey)!
//如果此动画已在运行或没有帧,我们将退出
如果animation | | animationFrames.count小于1
{
返回
}
变量
animationFrames.count
是一个数组,不会引发任何错误。 确切的错误是'Type'SkAction不符合协议布尔类型'

提前谢谢。

你需要写信

var animAction:SKAction!=player.actionForKey(动画键)

如果有行动!=无| | animationFrames.count<1{

return 
}

错误发生在animation上,而不是animationFrames

对于hasActions,它仍然存在于Swift中

var node=SKNode()

node.hasActions()


使用Swift 2.x您可以轻松执行以下操作:

public extension SKNode {
    public func actionForKeyIsRunning(key: String) -> Bool {
        return self.actionForKey(key) != nil ? true : false
    }
}
例如,你可以写:

var myHero: SKSpriteNode!
...
if myHero.actionForKeyIsRunning("moveLeft") {
   // my character moving to the left
   print("moving to the left")
} 

animAction
需要是可选类型,以对照
nil
进行检查。您要检查
是否为动画!=无…
我将==编辑为!=,很抱歉。变量不必是可选的,以对照nil进行检查。变量不能是
nil
,除非它是可选的。是
toto
是一个隐式展开的可选选项。这是可选的。原始海报的
animAction
不是可选的。您还需要删除最后一个!因为原始代码正在解包可选项,如果为nil,则会崩溃。