Friday, 11 September 2009

Change movie clip colour in Actionscript 3

In this Flash tutorial you will learn how to change the colour of a movie clip in Actionscript 3.0 using the ColorTransform class. A colour picker component will be used to change the colour of the movie clip. Some knowledge of basic buttons will also be needed for this tutorial.


Change movie clip colour in Actionscript 3

Step 1

Open a new Flash AS3 file.
Select the Rectangle Tool and drag a rectangle shape on the stage like below. Make sure your rectangle has no stroke.




Step 2

Convert your rectangle shape into a movie clip (F8), and then give the instance name: rectangle_mc.




Step 3

Select Window > Components and drag a ‘button’ and ‘ColorPicker’ components onto the stage.




Give the following instance name to the components accordingly: ‘change_btn’ and ‘colorPicker’. I have also changed the button label to: Change colour.


Step 4

On the timeline create a new layer called Actions. Then select the first frame and hit F9 to open up the actions panel and enter the following code:

//Creates a new instance of the ColorTrasform class.
var myColorTransform:ColorTransform=transform.colorTransform;

change_btn.addEventListener(MouseEvent.CLICK, changeColour);

function changeColour(event:MouseEvent):void {

//An instance of selected colour from the colour picker.
var theColor:uint=colorPicker.selectedColor;

//Sets the new colour from the colour picker.
myColorTransform.color=theColor;

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

}

Step 5

Test your movie clip Ctrl + Enter. Now try changing the colour of the move clip by selecting a colour from the colour picker, and clicking the change button.



You should now be able to change the colour of a movie clip in Actionscript 3.0.

0 comments:

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP