在SpriteKit中使用CCBlinkTo

时间:2014-03-14 19:26:09

标签: objective-c swift sprite-kit

如何在Sprite-Kit中实现类似CCBlink的操作?我希望2闪烁10次。

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

2 个答案:

答案 0 :(得分:6)

此示例使用alpha属性创建闪烁效果。您也可以使用hidden属性和runBlock操作,如@ LearnCocos2D建议的那样。

闪烁时间: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];
    }];

答案 1 :(得分:0)

我已经转换为 Swift 2.x S.E.答案,但我想要在闪烁期间着色我的节点。问题是当你必须处理不同类型的节点时。

我用这段代码解决了这个扩展:

// 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)
    }
}