Forum Topic: AS: Dynamically changing colors

(2,933 views • 29 replies)

This topic is 1 page long.

<< < > >>
None

Rantzien

Reply To Post Reply & Quote

Posted at: 8/25/05 05:08 PM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,426

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

None

Denvish

Reply To Post Reply & Quote

Posted at: 8/26/05 08:13 AM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

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

None

Rantzien

Reply To Post Reply & Quote

Posted at: 8/26/05 08:16 AM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,426

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

BBS Signature

None

Mogly

Reply To Post Reply & Quote

Posted at: 8/26/05 10:06 AM

Mogly LIGHT LEVEL 25

Sign-Up: 09/05/04

Posts: 10,336

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

~ MogTom ~ Dont fuck around with my dog. All that I can see I steal. ~
NG FFR ~ Automatic for the people.

BBS Signature

None

noob-toast

Reply To Post Reply & Quote

Posted at: 8/26/05 11:12 AM

noob-toast NEUTRAL LEVEL 08

Sign-Up: 05/18/04

Posts: 1,288

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

None

Rantzien

Reply To Post Reply & Quote

Posted at: 8/26/05 04:30 PM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,426

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

None

Denvish

Reply To Post Reply & Quote

Posted at: 8/26/05 04:33 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

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

None

Rantzien

Reply To Post Reply & Quote

Posted at: 8/26/05 05:00 PM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,426

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

None

Denvish

Reply To Post Reply & Quote

Posted at: 8/26/05 05:03 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

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

None

Rantzien

Reply To Post Reply & Quote

Posted at: 8/26/05 05:11 PM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,426

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

None

Starogre

Reply To Post Reply & Quote

Posted at: 8/26/05 05:26 PM

Starogre NEUTRAL LEVEL 18

Sign-Up: 05/08/04

Posts: 1,694

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

BBS Signature

None

Rantzien

Reply To Post Reply & Quote

Posted at: 8/26/05 05:31 PM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,426

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

None

Starogre

Reply To Post Reply & Quote

Posted at: 8/26/05 05:33 PM

Starogre NEUTRAL LEVEL 18

Sign-Up: 05/08/04

Posts: 1,694

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

BBS Signature

None

Rantzien

Reply To Post Reply & Quote

Posted at: 8/26/05 05:37 PM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,426

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

None

Starogre

Reply To Post Reply & Quote

Posted at: 8/26/05 05:39 PM

Starogre NEUTRAL LEVEL 18

Sign-Up: 05/08/04

Posts: 1,694

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.

BBS Signature

None

BlackmarketKraig

Reply To Post Reply & Quote

Posted at: 8/26/05 05:46 PM

BlackmarketKraig NEUTRAL LEVEL 32

Sign-Up: 12/08/04

Posts: 7,511

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

: [ fl ] : + : [ art ] : + : [ dA ] : + : [ ms ] : + : [ <3 ] :
. l . o . v . e .

BBS Signature

None

Rantzien

Reply To Post Reply & Quote

Posted at: 8/26/05 05:46 PM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,426

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

None

Starogre

Reply To Post Reply & Quote

Posted at: 8/26/05 05:48 PM

Starogre NEUTRAL LEVEL 18

Sign-Up: 05/08/04

Posts: 1,694

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

BBS Signature

None

Rantzien

Reply To Post Reply & Quote

Posted at: 8/26/05 05:51 PM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,426

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

None

Rantzien

Reply To Post Reply & Quote

Posted at: 8/26/05 06:25 PM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,426

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

None

BlackmarketKraig

Reply To Post Reply & Quote

Posted at: 8/26/05 07:33 PM

BlackmarketKraig NEUTRAL LEVEL 32

Sign-Up: 12/08/04

Posts: 7,511

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...?

: [ fl ] : + : [ art ] : + : [ dA ] : + : [ ms ] : + : [ <3 ] :
. l . o . v . e .

BBS Signature

None

Rantzien

Reply To Post Reply & Quote

Posted at: 8/26/05 07:36 PM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,426

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

None

piefi

Reply To Post Reply & Quote

Posted at: 10/1/05 10:01 AM

piefi EVIL LEVEL 07

Sign-Up: 05/07/05

Posts: 371

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


None

GuyWithHisComp

Reply To Post Reply & Quote

Posted at: 11/30/05 12:46 PM

GuyWithHisComp LIGHT LEVEL 27

Sign-Up: 11/10/05

Posts: 4,010

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

None

4as

Reply To Post Reply & Quote

Posted at: 11/30/05 03:50 PM

4as EVIL LEVEL 20

Sign-Up: 11/18/04

Posts: 32

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);
}


Happy

phyconinja

Reply To Post Reply & Quote

Posted at: 6/23/06 11:35 AM

phyconinja EVIL LEVEL 25

Sign-Up: 09/18/04

Posts: 2,828

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

good tutorial by the way!!
=)


Happy

Cema

Reply To Post Reply & Quote

Posted at: 6/23/06 11:36 AM

Cema LIGHT LEVEL 13

Sign-Up: 07/28/05

Posts: 1,941

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

BBS Signature

Expressionless

mayoarm11

Reply To Post Reply & Quote

Posted at: 11/15/07 06:55 AM

mayoarm11 NEUTRAL LEVEL 17

Sign-Up: 06/13/07

Posts: 1,013

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

None

Brycearoni

Reply To Post Reply & Quote

Posted at: 6/1/08 04:55 PM

Brycearoni NEUTRAL LEVEL 10

Sign-Up: 05/03/07

Posts: 229

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!


None

nobody404

Reply To Post Reply & Quote

Posted at: 6/1/08 04:56 PM

nobody404 NEUTRAL LEVEL 11

Sign-Up: 04/27/07

Posts: 279

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

Yet another half-year old revivial.

Bump for fail.


All times are Eastern Standard Time (GMT -5) | Current Time: 12:15 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!