Opacity animation of CCDrawNode (aka flashing)

How can you animate the opacity of certain - or all - objects drawn by a CCDrawNode?
If I do myDrawNode.AddAction(new CCMoveBy(2, new CCPoint(10, 0))) it works just fine. But when I implement my custom animation to animate the opacity - cause I wanna make my primitives kind of flash/blink - nothing happens. And CCDrawNode.Color is a CCColor3B, which doesn't have an alpha channel, yet when you call the .DrawRect or DrawCircle methods, you can specify a CCColor4B. This is my custom animation that's not working:

`public class OpacityAction : CCFiniteTimeAction
{
private readonly int endOpacityValue;

    public override CCFiniteTimeAction Reverse()
    {
        throw new NotImplementedException();
    }

    public OpacityAction(float duration, int endOpacityValue) : base(duration)
    {
        this.endOpacityValue = endOpacityValue;
    }

    protected override CCActionState StartAction(CCNode targetNode)
    {
        return new MazeColorState(this, targetNode, this.endOpacityValue);
    }
}`

`public class OpacityState : CCFiniteTimeActionState
{
private readonly int _startOpacity;

    private readonly int _opacityDelta;

    private CCDrawNode _theDrawNode;

    public OpacityState(CCFiniteTimeAction action, CCNode target, int endOpacityValue) : base(action, target)
    {
        _theDrawNode = target as CCDrawNode;            
        if (target == null)
        {
            throw new InvalidOperationException("Target must be a CCDrawNode");
        }

        // even if I set the IsColorModifiedByOpacity property to true, nothing happens!
        target.IsColorModifiedByOpacity = true;
        target.IsOpacityCascaded = true;
        _startOpacity = _theDrawNode.Opacity;
        _opacityDelta = endOpacityValue - _startOpacity;
    }

    public override void Update(float time)
    {
        _theDrawNode.Opacity = (byte)(_startOpacity + _opacityDelta * time);
    }
}`

Any ideas?
I'm using CocosSharp 1.7.1.0 and I debug my app on Android.

Best Answer

  • MatExMatEx NL ✭✭
    Accepted Answer

    OK guys, I found the solution: this does the trick:
    public override void Update(float time) { _theDrawNode.Clear(); _theDrawNode.DrawRect(_rect, new CCColor4B((byte)(_startOpacity + _opacityDelta * time, 0, 0, 255)); }

Answers

  • MatExMatEx NLMember ✭✭
    Accepted Answer

    OK guys, I found the solution: this does the trick:
    public override void Update(float time) { _theDrawNode.Clear(); _theDrawNode.DrawRect(_rect, new CCColor4B((byte)(_startOpacity + _opacityDelta * time, 0, 0, 255)); }

Sign In or Register to comment.