Thursday, 7 January 2010

Change movie clip colour randomly in AS3 – part 2

This is part 2 of the Change movie clip colour randomly in AS3 where the movie clip will change colour without a button. I will be using the Timer class to change the colour every two seconds. Make sure you have completed step 1 and step 2 of the previous tutorial as I will only be providing the code.


Change movie clip colour randomly in AS3 – part 2

Step 1

On the timeline create a new layer called 'Actions' then open the actions panel and add the following code.


//Create a new instance of the timer class with the delay set at
//2000 milliseconds (2seconds)
var myTimer:Timer=new Timer(2000);

//Add an eventlistener to the timer object.
myTimer.addEventListener(TimerEvent.TIMER, changeColour);

//Start the timer object.
myTimer.start();

function changeColour(event:TimerEvent):void {
//Creates a new instance of the ColorTrasform class.
var myColorTransform:ColorTransform = new ColorTransform();

//Sets the new random colour using the Math.random().
myColorTransform.color = (Math.random() * 0xFFFFFF);

//Apply the colour changes to the movie clip.
oval_mc.transform.colorTransform=myColorTransform;

}

Step 2

Test your movie clip Ctrl + Enter. You movie clip should now change colour randomly.

0 comments:

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP