How to use perform method on SKAction in Swift

276 Views Asked by At

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

2

There are 2 best solutions below

0
EranKT On BEST ANSWER

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 :)

0
Somebuddy On

If I understand your Problem right i think you're just trying to find a function like this:

blinkLightByOrder(onSprite: currentLight)

func blinkLightByOrder(onSprite: SKSpriteNode){
    onSprite.run(action4)
}

It worked for me and is just a simple call function