iosswiftskactionperformselector

How to use perform method on SKAction in Swift


I try to call a function using perform(_:onTarget:) as part of a sequence of SKAction.

I've tried:

let action1 = SKAction.fadeIn(withDuration: TimeInterval(0.2))
let action2 = SKAction.wait(forDuration: TimeInterval(0.4))
let action3 = SKAction.fadeOut(withDuration: TimeInterval(0.1))
let action4 = SKAction.perform(self.blinkLightByOrder, onTarget: UFOSprite._ufoBase)
let action5 = SKAction.sequence([action1, action2, action3, action4])

currentLight.run(action5)

When I use SKAction.run(block: () -> Void) like that:

let action4 = SKAction.run(self.blinkLightByOrder)

it works, but I need to send object as this method ask for SKSpriteNode:

func blinkLightByOrder(onSprite: SKSpriteNode)

but I keep getting error, and can't understand how to implement this perform action. Thanks


Solution

  • The answer posted in the comments by Orkhan Alikhanov

    SKAction.run { self.blinkLightByOrder(onSprite: mySprite) }
    

    worked great!

    Now I changed the method like this:

           currentLight.run(
            SKAction.run {
                SKAction.sequence([
                    SKAction.fadeIn(withDuration: TimeInterval(0.2)),
                    SKAction.wait(forDuration: TimeInterval(0.4)),
                    SKAction.fadeOut(withDuration: TimeInterval(0.1)),
                SKAction.run{ self.blinkLightByOrder() }]
                )
        })
    

    and it works just as expected! Thanks a lot :)