AS Color tween & ColorTransform Object

Hello,

I found this old thread and tried using both codes to color tween. I could not get either work. I know its probably very simple but I am very new to flash. I have attached the .fla. If anyone can get both example of the color tweens to work so I can understand how these function better would help greatly.

Also I would like to hear pros and cons on wither method.

http://www.kirupa.com/forum/showthread.php?t=210435

Thank you

import mx.transitions.Tween;
import mx.transitions.easing.*;
import flash.filters.ColorMatrixFilter;
import flash.geom.ColorTransform;
import flash.geom.Transform;
for (var i = 0; i < 1; i++) {
 var yb:Button = (this.attachMovie ("yelBox", "yelBox", +i, i));
 this._x = 275;
 this._y = 200;
 this._xscale = 50;
 this._yscale = 25;
}
for (var i = 0; i < 1; i++) {
 yb.onRelease = function () {
  var currentx:Number = this._x;
  new Tween (this, "_width", Back.easeInOut, 100, 200, 2, true);
  //colorTween
  var colour:Color = new Color (myMC);
  colour.setRGB (0xFFFFFF);
  var trans:Object = colour.getTransform ();
  var rb:Tween = new Tween (this, "rb", Strong.easeOut, trans.rb, Math.random () * 0xFF, 3, true);
  var gb:Tween = new Tween (this, "gb", Strong.easeOut, trans.gb, Math.random () * 0xFF, 3, true);
  var bb:Tween = new Tween (this, "bb", Strong.easeOut, trans.bb, Math.random () * 0xFF, 3, true);
  rb.onMotionChanged = function ():Void  {
   colour.setTransform (trans);
  };
 };
}
//color transform object starts here
/*import flash.geom.ColorTransform;
import flash.geom.Transform;
import mx.transitions.Tween;
import mx.transitions.easing.*;
//
var colorTrans:ColorTransform = new ColorTransform();
colorTrans.rgb = 0xFFFFFF;
var trans:Transform = new Transform(my_mc);
this.onMouseDown = function() {
    colorTween(my_mc, colorTrans, trans, 1, 0x00, 0xFF, 0x80, 0x00, Strong.easeOut);
};
//
function colorTween(mc:MovieClip, ct:ColorTransform, t:Transform, seconds:Number, a:Number, r:Number, g:Number, b:Number, ease:Function):Void {
    var alphaTween:Tween = new Tween(ct, "alphaOffset", ease, ct.alphaOffset, a, seconds, true);
    var redTween:Tween = new Tween(ct, "redOffset", ease, ct.redOffset, r, seconds, true);
    var greenTween:Tween = new Tween(ct, "greenOffset", ease, ct.greenOffset, g, seconds, true);
    var blueTween:Tween = new Tween(ct, "blueOffset", ease, ct.blueOffset, b, seconds, true);
    greenTween.onMotionChanged = function() {
        t.colorTransform = ct;
    };
    greenTween.onMotionFinished = function() {
        trace(mc._name + " is now 0x" + colorTrans.rgb.toString(16));
    };
}*/

The two different methods are pretty much exactly the same except that devonair’s script uses Flash 8’s ColorTransform object whereas mine uses the deprecated Color object.

import mx.transitions.Tween;
import mx.transitions.easing.*;
import flash.geom.ColorTransform;
import mx.utils.Delegate;
var yb:Button = this.attachMovie("yelBox", "yelBox", +i, i);
yb._x = 275;
yb._y = 200;
yb._xscale = 50;
yb._yscale = 25;
yb.onRelease = function() {
 //colorTween
 var c:ColorTransform = this.transform.colorTransform;
 var rb:Tween = new Tween(c, "redOffset", Strong.easeOut, c.redOffset, Math.random() * 0xFF, 3, true);
 var gb:Tween = new Tween(c, "greenOffset", Strong.easeOut, c.greenOffset, Math.random() * 0xFF, 3, true);
 var bb:Tween = new Tween(c, "blueOffset", Strong.easeOut, c.blueOffset, Math.random() * 0xFF, 3, true);
 rb.onMotionChanged = Delegate.create(this, function ():Void {
  this.transform.colorTransform = c;
 });
};

:hoser:

Would you be able to explain this block of code:

  
rb.onMotionChanged = Delegate.create(this, function ():Void {
                this.transform.colorTransform = c;
   });

I’m don’t even really need the code for anything, I’m just trying to understand what it means :elderly:

Delegate is a class in the mx.utils package. It creates the function in the scope of another object using Function.apply. In this example without delegating the function it will run in the scope of rb (the Tween). By using Delegate we can execute it in the scope of this (the movie clip yb) so we can easily reference its transform property. You can find more info in the Component Language Reference in the Help File. Hope this helps :hoser:

TheCanadian]The two different methods are pretty much exactly the same except that devonair’s script uses Flash 8’s ColorTransform object whereas mine uses the deprecated Color object.

Any pros and cons?

Thanks for the code.

Yep, it does! I wasn’t even aware the class existed. I guess I should read through the help files more to find out about these things. Thanks a lot for the help :toad:

Not really - if you’re using Flash 8, use the ColorTransform class :cowboy:

I am using F8.
I just wanted to make sure there wasn’t any other issues I should know about in advance.