I\'m new to Swift and SpriteKit. A lot of the samples of SpriteKit Actions are in Objective C, which I can\'t map to, nor get working, in Swift.
If running an SKAct
In Swift you can use this extension:
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 )
}
}
}
Use for example as:
myShip.runAction(move,withKey:"swipeMove",optionalCompletion: {
//Here action is ended..do whatever you want
}
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
}
}
Usage:
myShip.runAction(action: move, withKey:"swipeMove", optionalCompletion: {
//Here action is ended..do whatever you want
}