Home > ActionScript 3.0 Basics > Random color transform with AS3

Random color transform with AS3



In this quick tutorial I will show you how to change the color of a Movie Clip using the Color Transform class and Math.random.

1. Open a new Flash CS3 document with size 400 x 100.
2. Create your logo on the stage. I made the Flashadvanced logo. Give it an instance name “logo_mc”.
3. Create a new layer called “button”. Take the rectangle tool and draw a rectangle for your button. Select it and convert it to Button symbol with instance name “change_btn”.
4. Now we move to ActionScript:


//we import transform classes
import flash.geom.ColorTransform;
import flash.geom.Transform;

//variable for the color value
var col:uint;
//creating a color transform object
var cTransform:ColorTransform = transform.colorTransform;

//when clicking the button we change the color
change_btn.addEventListener(MouseEvent.CLICK, changeColor);

function changeColor(e:MouseEvent):void{
	//random color value
	col = Math.random()*0xffffff;
	cTransform.color = col;
	logo_mc.transform.colorTransform = cTransform;
}

That was quick and easy, right?

This is the final result

Liked this tutorial? Share and Enjoy:

  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Reddit
  • blogmarks
  • StumbleUpon
  • Technorati
  • TwitThis
  • Yahoo! Buzz
  • NewsVine
  • Slashdot
  1. flashman
    September 11th, 2009 at 01:38 | #1

    Is the ‘logo_mc’ necessary in the following line of code:
    var cTransform:ColorTransform = logo_mc.transform.colorTransform;

    Should it not be the following:

    var cTransform:ColorTransform = transform.colorTransform;

  2. admin
    September 11th, 2009 at 07:47 | #2

    Hey,
    thank you for the comment.
    And you are actually right – there is no need to type “logo_mc” in front of “transform.colorTransform”.

  1. No trackbacks yet.