简体   繁体   English

在SpriteKit中使用CCBlinkTo

[英]CCBlinkTo in SpriteKit

How can I achieve CCBlink like action in Sprite-Kit ? 如何在Sprite-Kit中实现类似CCBlink的操作? I want 10 blink in 2. 我希望2闪烁10次。

id blink = [CCBlink actionWithDuration:2.0f blinks:10];
id calBck = [CCCallBlock actionWithBlock:^{
           [enemy expired];
    }];
[HeroSprite runAction:[CCSequence actions:blink, calBck, nil]];

This example uses the alpha property to create the blink effect. 此示例使用alpha属性创建闪烁效果。 You might as well use the hidden property and a runBlock action, as @LearnCocos2D suggested. 您也可以使用hidden属性和runBlock操作,如@ LearnCocos2D建议的那样。

Blink time: 2.0 / 10 = 0.2, so 0.1 seconds for each fade-in and fade-out. 闪烁时间:2.0 / 10 = 0.2,每次淡入和淡出都是0.1秒。

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

I've translated to Swift 2.x the SE answer but I want also colorize my node during the blinking. 我已经将SE答案转换为Swift 2.x但是我想在眨眼期间为我的节点着色 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)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM