SKAction Completion Handlers; usage in Swift

不羁的心 提交于 2019-11-30 08:10:08

问题


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 SKAction, and upon SKAction completion wanting to do something else, how do I get this right, in Swift?

    spaceMan.runAction(spaceManDeathAnimation, completion: {
        println("red box has faded out")
    })

Any ideas will be greatly appreciated.

Edit:

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

回答1:


You completion code is not called since your "death" action is running forever, which means it will never end.

You can use

+ repeatAction:count:

method for setting a count for how many repeats will be made before finishing:

spaceManDeathAnimation = SKAction.repeatAction(SKAction.animateWithTextures(textures, timePerFrame: 0.15625), count:5)



回答2:


Found an issue here:

spaceManDeathAnimation = SKAction.repeatAction(SKAction.animateWithTextures(textures, timePerFrame: 0.15625), count: 1)

Also,as sangony posted a very nice link - solved the completion block syntax to

    spaceMan.runAction(spaceManDeathAnimation, completion: {() -> Void in
        println("death")
    })

Very big thanks to everybody for contributions for a solution!




回答3:


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
}

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
    }
}

Usage:

myShip.runAction(action: move, withKey:"swipeMove", optionalCompletion: {
       //Here action is ended..do whatever you want
}


来源:https://stackoverflow.com/questions/29627613/skaction-completion-handlers-usage-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!