Groups | Blog | Home
all groups > flash actionscript > march 2006 >

flash actionscript : dynamically changing the color of a movie clip



athomas32
3/14/2006 10:40:23 PM
I am creating buttons from the info that is loading in by a xml file. The xml
file passes in a color for each button. this is all created dynamically, there
could be any number of buttons. Just depends on the xml. So i need a way to
have all of the buttons be different colors. How would go about this??? This
is driving me to drink :beer;
blemmo
3/15/2006 3:08:43 PM
Check out the flash.geom.Transform class. Every MC has a 'transform' property,
which you can replace with a custom Transform object that holds new colors.
Example:
--
import flash.geom.Transform;
import flash.geom.ColorTransform;

// some MC
var rect:MovieClip = createRectangle(20, 80, 0xFF0000);

// get the Transform object of that MC
var trans:Transform = new Transform(rect);
trace(trans.colorTransform);
// output: redMultiplier=1, greenMultiplier=1, blueMultiplier=1,
alphaMultiplier=1, redOffset=0, greenOffset=0, blueOffset=0, alphaOffset=0

// create new ColorTransform object for 'blue'
var blueColorTransform:ColorTransform = new ColorTransform(0, 1, 1, 1, 0, 0,
255, 0);

// assign the 'blue' ColorTransform to the MC's Transform property
trans.colorTransform = blueColorTransform;
--
The 'offset' parameters of ColorTransform can be set to rgb values.

This will need Flash 8. It's also possible for older versions, but it's a
different method there.

hth,
blemmo
Rothrock
3/15/2006 3:23:00 PM
Under earlier versions it is very much the same just a different syntax. You
use the color class setTransform(). I've got an experiment that lets you play
with it. First read the help files, then visit this link:

http://www.saffronthread.com/exp/colorTrans.swf

Drag the white dot onto different parts of the stage (there are several
different clips). The dials will read the current transform applied and then
you can adjust to see the effects. I found it helped me get a feel for the
kinda abstract numbers that are used.

While I haven't worked with the new colorTransform class, it seems to work on
pretty much the same kind of idea.
athomas32
3/16/2006 8:36:56 PM
i am tring this with a mx.controls. button

Will it work with that? In the help file is says that you have to skin the
button. I have walked through this and am not seeming to get anywhere. Do you
know of any good tutorials that would help me through this?
blemmo
3/16/2006 9:04:06 PM
Well, I guess this works only with MCs. With components, it's possible to
assign some styles with AS, e.g. someButton.setStyle("color", 0x990000) should
set the text color of the button to a dark red, but you can't set all
properties of the button that way. It might be that you have to do a custom
skin, but I never did that, and I'm not sure if it helps you with the dynamic
changes...
You could also think about using MCs as buttons, then you could use the ways
explained above.

cheers,
blemmo
Rothrock
3/16/2006 9:14:33 PM
Good lord/bad lord! Why would you use a button component? MM supplied
components are too big, awkward, and cause all sorts of problems in the end.
Especially for something as easy as a button.

If you really need fancy button, search the forums, NSurveyor has posted code
which does basically everything the MM (I just can't call it Adobe yet!)
buttons do. But his code is 1K.
athomas32
3/16/2006 9:52:55 PM
Thank you guys for your imput. I am crazy, this i now. I am trying to finish
up a project that someone else started and i am slowly finding all of the
things that he put in there are not smart. Thank you for helping, i am just
learning actionscript and it has been hard just jumping into a project like
this.

I realize making my own button would be the best way. I am just not sure how
to create it dynamically.
Would i just create a new movieClip with actionscript? This will work
everytime i go through the for loop.
blemmo
3/17/2006 3:27:00 AM
The easiest way is a MovieClip in the library, designed as you wish (e.g. with
another MC in it that gives the background color), and given a linkage ID. Then
you could do
attachMovie("linkageID","stageID",depth)
to get it on stage. You could then add the click events and set the color of
the background MC via code. Note that if you do that inside the MC, with code
on its own timeline, it would take effect only after all code from the calling
frame (in the parent timeline) is executed, so you can't use these from there.
May be irritating when its happening, so be warned. Better set the properties
from the parent timeline after attaching the MC.

You could also write a class that defines your button, and create an instance
of that. Here it may be tricky to attach it to the stage, but there are some
forum posts dealing with that. This may be a bit hard for starters, so I'd
recommend the first way, that should be a lot easier.

greets,
blemmo
AddThis Social Bookmark Button