在 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
}
斯威夫特 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
}