0
votes

I try to set an extension like SKAction.fireFromEnemy(), and append to the action sequence, and reuse the sequence. My trouble is I can't get the real self so I have to set the sequence every time I create the enemy.

Like a SKAction.removeFromParent(), which func can target the node runs the action.

2

2 Answers

2
votes

SKAction.customAction is probably what you want.

https://developer.apple.com/documentation/spritekit/skaction/1417745-customaction

Set the duration to 0 if you want it to happen one time.

SKAction.customAction(withDuration: 0) { node, time in node.fireFromEnemy() }

0
votes

Real self is on closure as a fist parameter of customAction

Example:

class TestClass {
    init() {
        let node = SKSpriteNode(color:.red,size:CGSize(width:100,height:100))
        let sequence = SKAction.sequence([.fireFromEnemy, .wait(forDuration: 10), .removeFromParent()])
        node.run(sequence)
    }
}

extension SKAction {
    static let fireFromEnemy = SKAction.customAction(withDuration: 0.3) { node, elapsedTime in
        // Do stuff here like:
        node.alpha = 0.5
        node.zRotation = .pi
    }
}