00:00
00:00

AS: Dynamically changing colors

4,533 Views | 29 Replies
New Topic Respond to this Topic

AS: Dynamically changing colors 2005-08-25 17:08:15


AS: Main

Dynamically changing colors

Here is an example of the use for this. Just a simple effect achieved by using the Color class. I won't explain the code, unless you ask for it of course.

First, draw a circle or something that you want to change colors on. Remember that the code we will write will change the color of the whole Movie Clip, so if you are going to do shades and lines like in the example, do it separately later on.

Turn what you drew into a Movie Clip (F8 > Movie Clip > OK). Go to the Properties panel (Ctrl+F3), and in the box where it says "<Instance Name>", enter "myShape" (without the quotes).

Now, we will make a new color object. Add this to the frame's actionscript:
var myColor:Color = new Color(_root.myShape);

Then, draw a button. Make it a button (F8 > Button > OK), and add this to it's actionscript:
on (release) {
balloon_color.setRGB(0xFF0000);
}

Now, if you test the movie and click the button, the shape you drew should turn red (if, of course, it wasn't red already).

Related AS threads:
AS: Variables
AS: OOP (Object Oriented Programming) by Inglor
AS: Photo Negative by Glaiel_Gamer


BBS Signature

Response to AS: Dynamically changing colors 2005-08-26 08:13:57


At 8/25/05 05:08 PM, Rantzien wrote: Dynamically changing colors

Yeah, I use this often, it's definitely a very useful thing to know.
Normally, I just use this on the MC itself when I want to switch colours:

new Color(this).setRGB(0xFF0000);

Your way is clearer to understand though.


- - Flash - Music - Images - -

BBS Signature

Response to AS: Dynamically changing colors 2005-08-26 08:16:50


Thank you Denvish! It's great to get comments and criticism, both good and bad.


BBS Signature

Response to AS: Dynamically changing colors 2005-08-26 10:06:18


is there a way of using this for everytime an animation loops that it will change its colour?


WEBSITE

BLOG ~ Dont fuck around with my dog. All that I can see I steal. ~

NG FFR ~ Automatic for the people.

BBS Signature

Response to AS: Dynamically changing colors 2005-08-26 11:12:11


I guess it'd be something like setting up an array of colors, and at the end of the animation you'd take a random number from the array and set the mc's color to it.


These new signatures can suck on mah balls. My lolis don't fit in. Lol wut what are you guys still doing on NG, move on.

BBS Signature

Response to AS: Dynamically changing colors 2005-08-26 16:30:24


At 8/26/05 10:06 AM, Mogly wrote: is there a way of using this for everytime an animation loops that it will change its colour?

Yes, you can do as the other guy said, like this:
On the first frame:

clrs = [ "0x000000", "0x333333", "0x666666", "0x999999", "0xCCCCCC", "0xFFFFFF" ];

On the first frame of the loop:

myColor.setRGB(_root.clrs[Math.ceil(Math.r
andom()*_root.clrs.length)]);

The problem (or not?) with this is that the avaliable colors is only the ones you specify in the array. I think there's a way to randomize colors, but I think that involves hexadecimals, which I'm not very familiar with.


BBS Signature

Response to AS: Dynamically changing colors 2005-08-26 16:33:42


At 8/26/05 04:30 PM, Rantzien wrote: myColor.setRGB(_root.clrs[Math.ceil(Math.r

andom()*_root.clrs.length)]);

Why not just myColor.setRGB( _root.clrs[random(_root.clrs.length)]);?


- - Flash - Music - Images - -

BBS Signature

Response to AS: Dynamically changing colors 2005-08-26 17:00:47


At 8/26/05 04:33 PM, Denvish wrote:
At 8/26/05 04:30 PM, Rantzien wrote: myColor.setRGB(_root.clrs[Math.ceil(Math.r
andom()*_root.clrs.length)]);

Why not just myColor.setRGB( _root.clrs[random(_root.clrs.length)]);?

Because my way is more up-to-date =P
I know that they do the same thing in this particular case, but it's just a matter of principle.


BBS Signature

Response to AS: Dynamically changing colors 2005-08-26 17:03:44


random() should never have been depreciated IMO. It performs a seperate and perfectly valid alternative to Math.random() (ie, returning an integer). A large number of coders (including myself) still use it for that very reason.


- - Flash - Music - Images - -

BBS Signature

Response to AS: Dynamically changing colors 2005-08-26 17:11:30


At 8/26/05 05:03 PM, Denvish wrote: random() should never have been depreciated IMO. It performs a seperate and perfectly valid alternative to Math.random() (ie, returning an integer). A large number of coders (including myself) still use it for that very reason.

I know it's simpler and stuff, I guess the AS developers think something along the lines of: If two functions can do the same things, only the other one can do more, then deprecate the other one. I also guess that they prefer collecting many mathematical functions under one top-level class.

I'm not defending their case, I'm just reasoning.


BBS Signature

Response to AS: Dynamically changing colors 2005-08-26 17:26:35


There seems to be a variable misinterpretation in the tutorial you just wrote above. I'm not understanding...


the events are merely fictional, written, directed, and acted out by all who create them

BBS Signature

Response to AS: Dynamically changing colors 2005-08-26 17:31:34


At 8/26/05 05:26 PM, Starogre wrote: There seems to be a variable misinterpretation in the tutorial you just wrote above. I'm not understanding...

Oh shit. I made a fatal error. It shouldn't say balloon_color there, it should say myColor! Goddamn it!

Thanks for notifying me anyway.


BBS Signature

Response to AS: Dynamically changing colors 2005-08-26 17:33:42


Lol. I knew it, but it still doesn't work.


the events are merely fictional, written, directed, and acted out by all who create them

BBS Signature

Response to AS: Dynamically changing colors 2005-08-26 17:37:35


At 8/26/05 05:33 PM, Starogre wrote: Lol. I knew it, but it still doesn't work.

I need some kind of clue to have even the slightest idea of what the problem may be.


BBS Signature

Response to AS: Dynamically changing colors 2005-08-26 17:39:18


Frame 1:
var myColor:Color = new Color(_root.myShape);

Red Circle Button:
on (release) {
_root.myColor.setRGB(0xFF0000);
}

Blue Gradient MC:
Instance name: myShape

The button isn't changing the blue circle's/balloons color.


the events are merely fictional, written, directed, and acted out by all who create them

BBS Signature

Response to AS: Dynamically changing colors 2005-08-26 17:46:02


I'm also having problems getting it to work...


This sig is 100% effective protection from all hexes, curses, evil spirits and bad karma. Guaranteed.

BBS Signature

Response to AS: Dynamically changing colors 2005-08-26 17:46:10


At 8/26/05 05:39 PM, Starogre wrote: The button isn't changing the blue circle's/balloons color.

I copied and pasted all that info into mine, and it worked just fine. What version of Flash do you use? This function was implemented in Flash Player 5.


BBS Signature

Response to AS: Dynamically changing colors 2005-08-26 17:48:44


LOL MX here, well, i'm mad now >.> oh well...


the events are merely fictional, written, directed, and acted out by all who create them

BBS Signature

Response to AS: Dynamically changing colors 2005-08-26 17:51:18


At 8/26/05 05:48 PM, Starogre wrote: LOL MX here, well, i'm mad now >.> oh well...

Anyone knowing what the problem may be? I don't fancy having an AS thread that doesn't work for some.


BBS Signature

Response to AS: Dynamically changing colors 2005-08-26 18:25:17


Alright, the built-in Flash help tells you to do this. If this fails, you're probably doing something wrong.

1. Select a movie clip on the Stage.

2. In the Property inspector, enter carColor as the instance name.

3. Create a button named color chip, place four instances of the button on the Stage, and name them red, green, blue, and black.

4. Select Frame 1 in the main Timeline, and select Window > Development Panels > Actions.

5. To create a Color object that targets the carColor movie clip, add the following code to the Actions panel:
myColor = new Color(_root.carColor);

6. To make the blue button change the color of the carColor movie clip to blue, add the following code to the Actions panel:
_root.blue.onRelease = function(){
myColor.setRGB(0x0000ff)
}

The hexadecimal value 0x0000ff is blue. The following table displays the other colors you'll use and their hexadecimal values:

7. Repeat step 6 for the other buttons (red, green, and black) to change the color of the movie clip to the corresponding color. Your code should now look like this:
myColor = new Color(_root.carColor)
_root.blue.onRelease = function(){
myColor.setRGB(0x0000ff)
}
_root.red.onRelease = function(){
myColor.setRGB(0xff0000)
}
_root.green.onRelease = function(){
myColor.setRGB(0x00ff00)
}
_root.black.onRelease = function(){
myColor.setRGB(0x000000)
}

8. Select Control > Test Movie to change the color of the movie clip.


BBS Signature

Response to AS: Dynamically changing colors 2005-08-26 19:33:33


At 8/26/05 06:25 PM, Rantzien wrote: Alright, the built-in Flash help tells you to do this. If this fails, you're probably doing something wrong.

Blah blah blah 1. 2. 3. whatever...

Hey that one works...
Thanks! I guess I should have looked through the AS dictionary...

I wonder why the original one doesn't work for me...?


This sig is 100% effective protection from all hexes, curses, evil spirits and bad karma. Guaranteed.

BBS Signature

Response to AS: Dynamically changing colors 2005-08-26 19:36:18


At 8/26/05 07:33 PM, BlackmarketKraig wrote: I wonder why the original one doesn't work for me...?

Yep, that's weird. They're almost the exact same, I can't find something that would make a difference.


BBS Signature

Response to AS: Dynamically changing colors 2005-10-01 10:01:11


ah i wondered why the last code wasn't working.I put the instance name as colour(the english spelling) instead of color

Response to AS: Dynamically changing colors 2005-11-30 12:46:34


Yay look what I did :)
Just make a MC called myShape and place this in the frame AS:
var myColor:Color = new Color(_root.myShape);
onEnterFrame=function(){
myColor.setRGB(colour);
colour="0x"+random(999999);
}


BBS Signature

Response to AS: Dynamically changing colors 2005-11-30 15:50:39


At 11/30/05 12:46 PM, GuyWithHisComp wrote: Yay look what I did :)
Just make a MC called myShape and place this in the frame AS:
var myColor:Color = new Color(_root.myShape);
onEnterFrame=function(){
myColor.setRGB(colour);
colour="0x"+random(999999);
}

Well, your changing number into a string, and then string into a number...
Just do that instead:
var myColor:Color = new Color(_root.myShape);
onEnterFrame = function():Void {
myColor.setRGB(Math.random()*0xffffff);
}

Response to AS: Dynamically changing colors 2006-06-23 11:35:14


is there any way to use this to change the colour of the text in a dynamic textbox?

good tutorial by the way!!
=)

Response to AS: Dynamically changing colors 2006-06-23 11:36:47


Very nice tutorial. I will deffinetly use this sometime in the future.


BBS Signature

Response to AS: Dynamically changing colors 2007-11-15 06:55:27


how do i change the color not to a solid color, but to a gradient color (linear / radial)?


Awesome turret game in the making, featuring 85 uniqe turrets: News post

BBS Signature

Response to AS: Dynamically changing colors 2008-06-01 16:55:41


At 8/26/05 08:13 AM, Denvish wrote:
At 8/25/05 05:08 PM, Rantzien wrote: Dynamically changing colors
Yeah, I use this often, it's definitely a very useful thing to know.
Normally, I just use this on the MC itself when I want to switch colours:

new Color(this).setRGB(0xFF0000);

Your way is clearer to understand though.

thanks Denvish! (I think your way is easier) Also it was a nice tutorial tho!

Response to AS: Dynamically changing colors 2008-06-01 16:56:32


thanks Denvish! (I think your way is easier) Also it was a nice tutorial tho!

Yet another half-year old revivial.

Bump for fail.