5

How can I achieve CCBlink like action in Sprite-Kit ? I want 10 blink in 2.

id blink = [CCBlink actionWithDuration:2.0f blinks:10];
id calBck = [CCCallBlock actionWithBlock:^{
           [enemy expired];
    }];
[HeroSprite runAction:[CCSequence actions:blink, calBck, nil]];
2
  • use a runBlock action that toggles the hidden property
    – CodeSmile
    Mar 14, 2014 at 22:32
  • Or a sequence of very short fades.
    – rickster
    Mar 15, 2014 at 2:52

2 Answers 2

6

This example uses the alpha property to create the blink effect. You might as well use the hidden property and a runBlock action, as @LearnCocos2D suggested.

Blink time: 2.0 / 10 = 0.2, so 0.1 seconds for each fade-in and fade-out.

    HeroSprite.alpha = 0.0;

    SKAction *blinkSequence = [SKAction sequence:@[
        [SKAction fadeAlphaTo:1.0 duration:0.1],
        [SKAction fadeAlphaTo:0.0 duration:0.1]
    ]];


    [HeroSprite runAction:[SKAction repeatAction:blinkSequence count:10] completion:^{
        [enemy expired];
    }];
1

I've translated to Swift 2.x the S.E. answer but I want also colorize my node during the blinking. The problem was when you have to do with different types of node.

I've solved and make this extension with this code:

// SKAction extension : blink effect for a generic SKNode, SKShapeNode, SKSpriteNode and SKLabelNode
// Usage:
// self.runAction(SKAction.blink(5)) // fade in and fade out 5 times for 2 seconds
// self.runAction(SKAction.blink(5, duration: 3.0)) // fade in and fade out 5 times for 3 seconds or whatever you want
// self.runAction(SKAction.blink(5, duration: 3.0, colorize: true)) // fade in and fade out 5 times for 3 seconds switching color from red to white
// self.runAction(SKAction.blink(5, duration: 3.0, colorize: true, color1:SKColor.blueColor(), color2:SKColor.yellowColor())) // fade in and fade out 5 times for 3 seconds switching color from custom color2 to custom color1
extension SKAction {
    class func blink(times:Int,duration:NSTimeInterval = 2.0,colorize:Bool = false, color1:SKColor = SKColor.redColor(),color2:SKColor = SKColor.whiteColor())->SKAction {
        let fadeOut = SKAction.fadeAlphaTo(0.0, duration: (duration/Double(times))/2)
        let fadeIn = SKAction.fadeAlphaTo(1.0, duration: (duration/Double(times))/2)
        if colorize {
            var isColorChanged = false
            let colorize = SKAction.customActionWithDuration(0.01, actionBlock: {
                    node, elapsedTime in
                    switch node {
                    case is SKSpriteNode:
                        if !isColorChanged {
                            node.runAction(SKAction.colorizeWithColor(color2, colorBlendFactor: 1, duration: 0.01))
                        } else {
                            node.runAction(SKAction.colorizeWithColor(color1, colorBlendFactor: 1, duration: 0.01))
                        }
                    case is SKShapeNode:
                        (node as! SKShapeNode).fillColor = isColorChanged ? color1 : color2
                    case is SKLabelNode:
                        (node as! SKLabelNode).fontColor = isColorChanged ? color1 : color2
                    default:
                        break
                    }
            })
            let changeColor = SKAction.runBlock({
                isColorChanged = !isColorChanged
            })
            let blink = SKAction.sequence([fadeOut,colorize, fadeIn, changeColor])
            return SKAction.repeatAction(blink, count: times)
        }
        let blink = SKAction.sequence([fadeOut, fadeIn])
        return SKAction.repeatAction(blink, count: times)
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.