SKAction 完成处理程序; Swift 中的用法
SKAction Completion Handlers; usage in Swift
我是 Swift 和 SpriteKit 的新手。 Objective C 中有很多 SpriteKit 动作示例,我无法映射到 Swift 中,也无法工作。
如果 运行 一个 SKAction,并且在 SKAction 完成后想做其他事情,我如何在 Swift?
中做到这一点
spaceMan.runAction(spaceManDeathAnimation, completion: {
println("red box has faded out")
})
任何想法将不胜感激。
编辑:
for i in 0...29 {
textures.append(SKTexture(imageNamed: "spaceManDeath_\(i)"))
}
spaceManDeathAnimation = SKAction.repeatActionForever(SKAction.animateWithTextures(textures, timePerFrame: 0.15625))
在这里发现了一个问题:
spaceManDeathAnimation = SKAction.repeatAction(SKAction.animateWithTextures(textures, timePerFrame: 0.15625), count: 1)
此外,sangony 发布了一个非常好的 link - 将完成块语法解决为
spaceMan.runAction(spaceManDeathAnimation, completion: {() -> Void in
println("death")
})
非常感谢大家对解决方案的贡献!
不会调用您的完成代码,因为您的 "death" 操作将永远 运行,这意味着它永远不会结束。
您可以使用
+ repeatAction:count:
设置完成前重复次数的方法:
spaceManDeathAnimation = SKAction.repeatAction(SKAction.animateWithTextures(textures, timePerFrame: 0.15625), count:5)
在 Swift 中,您可以使用此扩展程序:
extension SKNode
{
func runAction( action: SKAction!, withKey: String!, optionalCompletion: dispatch_block_t? )
{
if let completion = optionalCompletion
{
let completionAction = SKAction.runBlock( completion )
let compositeAction = SKAction.sequence([ action, completionAction ])
runAction( compositeAction, withKey: withKey )
}
else
{
runAction( action, withKey: withKey )
}
}
}
例如用作:
myShip.runAction(move,withKey:"swipeMove",optionalCompletion: {
//Here action is ended..do whatever you want
}
Swift 3:
extension SKNode
{
func run(action: SKAction!, withKey: String!, optionalCompletion:((Void) -> Void)?) {
if let completion = optionalCompletion
{
let completionAction = SKAction.run(completion)
let compositeAction = SKAction.sequence([ action, completionAction ])
run(compositeAction, withKey: withKey )
}
else
{
run( action, withKey: withKey )
}
}
func actionForKeyIsRunning(key: String) -> Bool {
return self.action(forKey: key) != nil ? true : false
}
}
用法:
myShip.runAction(action: move, withKey:"swipeMove", optionalCompletion: {
//Here action is ended..do whatever you want
}
我是 Swift 和 SpriteKit 的新手。 Objective C 中有很多 SpriteKit 动作示例,我无法映射到 Swift 中,也无法工作。
如果 运行 一个 SKAction,并且在 SKAction 完成后想做其他事情,我如何在 Swift?
中做到这一点 spaceMan.runAction(spaceManDeathAnimation, completion: {
println("red box has faded out")
})
任何想法将不胜感激。
编辑:
for i in 0...29 {
textures.append(SKTexture(imageNamed: "spaceManDeath_\(i)"))
}
spaceManDeathAnimation = SKAction.repeatActionForever(SKAction.animateWithTextures(textures, timePerFrame: 0.15625))
在这里发现了一个问题:
spaceManDeathAnimation = SKAction.repeatAction(SKAction.animateWithTextures(textures, timePerFrame: 0.15625), count: 1)
此外,sangony 发布了一个非常好的 link - 将完成块语法解决为
spaceMan.runAction(spaceManDeathAnimation, completion: {() -> Void in
println("death")
})
非常感谢大家对解决方案的贡献!
不会调用您的完成代码,因为您的 "death" 操作将永远 运行,这意味着它永远不会结束。
您可以使用
+ repeatAction:count:
设置完成前重复次数的方法:
spaceManDeathAnimation = SKAction.repeatAction(SKAction.animateWithTextures(textures, timePerFrame: 0.15625), count:5)
在 Swift 中,您可以使用此扩展程序:
extension SKNode
{
func runAction( action: SKAction!, withKey: String!, optionalCompletion: dispatch_block_t? )
{
if let completion = optionalCompletion
{
let completionAction = SKAction.runBlock( completion )
let compositeAction = SKAction.sequence([ action, completionAction ])
runAction( compositeAction, withKey: withKey )
}
else
{
runAction( action, withKey: withKey )
}
}
}
例如用作:
myShip.runAction(move,withKey:"swipeMove",optionalCompletion: {
//Here action is ended..do whatever you want
}
Swift 3:
extension SKNode
{
func run(action: SKAction!, withKey: String!, optionalCompletion:((Void) -> Void)?) {
if let completion = optionalCompletion
{
let completionAction = SKAction.run(completion)
let compositeAction = SKAction.sequence([ action, completionAction ])
run(compositeAction, withKey: withKey )
}
else
{
run( action, withKey: withKey )
}
}
func actionForKeyIsRunning(key: String) -> Bool {
return self.action(forKey: key) != nil ? true : false
}
}
用法:
myShip.runAction(action: move, withKey:"swipeMove", optionalCompletion: {
//Here action is ended..do whatever you want
}