SKAction完井处理程序; 在Swift中的用法

spa*_*h21 11 handler sprite-kit skaction swift completion

我是Swift和SpriteKit的新手.很多SpriteKit动作的样本都在Objective C中,我无法在Swift中映射,也无法工作.

如果运行SKAction,并且在SKAction完成后想要做其他事情,我如何在Swift中做到这一点?

    spaceMan.runAction(spaceManDeathAnimation, completion: {
        println("red box has faded out")
    })
Run Code Online (Sandbox Code Playgroud)

任何想法将不胜感激.

编辑:

for i in 0...29 {
    textures.append(SKTexture(imageNamed: "spaceManDeath_\(i)"))
}
spaceManDeathAnimation = SKAction.repeatActionForever(SKAction.animateWithTextures(textures, timePerFrame: 0.15625))
Run Code Online (Sandbox Code Playgroud)

spa*_*h21 5

在这里发现了一个问题:

spaceManDeathAnimation = SKAction.repeatAction(SKAction.animateWithTextures(textures, timePerFrame: 0.15625), count: 1)
Run Code Online (Sandbox Code Playgroud)

此外,正如 sangony 发布了一个非常好的链接 - 解决了完成块语法

    spaceMan.runAction(spaceManDeathAnimation, completion: {() -> Void in
        println("death")
    })
Run Code Online (Sandbox Code Playgroud)

非常感谢大家对解决方案的贡献!


gio*_*shc 4

您的完成代码不会被调用,因为您的“死亡”操作将永远运行,这意味着它永远不会结束。

您可以使用

+ repeatAction:count:
Run Code Online (Sandbox Code Playgroud)

设置完成前重复次数的方法:

spaceManDeathAnimation = SKAction.repeatAction(SKAction.animateWithTextures(textures, timePerFrame: 0.15625), count:5)
Run Code Online (Sandbox Code Playgroud)