Best viewed in Firefox

Awesty Productions

Changing Color Through ActionScript

April 8th, 2007 by awesty

In this tutorials you will learn how to change a MCs (movie clip) color through actionscript. I am using flash 8, but flash MX 2004 will work fine as well.

Start off by making a new flash document and make a circle on the stage. Make sure it is a solid color, without an outline. Mine is read. Select and hit F8 to convert it to a movie clip.

Step 1

Once you have done that, give the MC an instance name of ‘colorful’. (Without the quotation marks :P )


Step 2

Now apply this code to the first frame:

onEnterFrame = function(){
    var color:Color = new Color("colorful");
    color.setRGB(0×00FF00);
}

Now if you test your movie (Ctrl/Cmd+Enter), the MC should be green instead of red (This wouldn’t of worked if you had of had your MC green in the first place). Now to explain the code.

The first line, as always, means everytime the frame is entered.
The next line is making a new color object, and its target is the MC ‘colorful’.
The next line sets the color of the color object to 00FF00, which is green. If you want to find the value for a color, click on the color changer to the left of the screen. When your mouse is over a color it will display that color. (setRGB(0xRRGGBB)).

Step 3

Red: FF0000, Blue: 0000FF, Green: 00FF00, White: FFFFFF, Black: 000000, Purple-Violety: B90DF2

Now what if you wanted to change the color on a key press or something? Paste this code onto the frame.

onEnterFrame = function(){
    var color:Color = new Color("colorful");
    if(Key.isDown(Key.SPACE)){
        color.setRGB(0×00FF00);
    }else{
        color.setRGB(0×0000FF);
    }
}

I don’t really think there is any need to explain that code, since it is practically the same as the last one.

Now if you test your movie you will get something like this:

Hit the spacebar to see the color change.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • del.icio.us
  • digg
  • Furl
  • MyShare
  • NewsVine
  • Netscape
  • Reddit
  • Simpy
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

RSS feed | Trackback URI

7 Comments »

Comment by hansa
2007-04-10 00:20:55

thanks a lot awesty

 
Comment by Matt
2007-04-13 14:47:20

Thanks thats really useful

 
Comment by Zane Subscribed to comments via email
2007-06-16 03:02:47

It Always Says:
**Error** Scene=Scene 1, layer=Layer 3, frame=1:Line 2: ‘)’ or ‘,’ expected
color.setRGB(0×FF0000)

Total ActionScript Errors: 1 Reported Errors: 1

Comment by awesty
2007-07-03 18:38:37

Delete the 0x then retype it again in flash.

 
 
Comment by Kurt Subscribed to comments via email
2007-06-23 09:35:13

Im just testing this ok.

Comment by Kurt Subscribed to comments via email
2007-06-23 09:37:10

Dont reply to this comment delete it or let it stay

 
 
Comment by David
2007-12-02 00:26:27

Nice tutorial, simple and easy to follow :D

 
Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> in your comment.