Groups | Blog | Home
all groups > flash actionscript > june 2004 >

flash actionscript : Changing color based on a Variable


Larry.russell
6/9/2004 6:20:34 PM
I am new to actionscript and I am having a bit of a problem. I have an oval
that I would like to change color based on a variable. Such as if Variable
s1=green the oval is green. Is there a way to do this?
silvernapalm
6/9/2004 6:41:36 PM
if you are using a button you could do something like this:
place this on the button
//s1= new Color(the object you want to change);

on (release) {
s1= new Color(oval);
s1.setRGB(0x00FF00);
}

Larry.russell
6/9/2004 7:10:44 PM
Right now I have a test file loading the variables. It looks like this:
&s1=green&s2=yellow&s3=green&s4=red

What I want to happen is when the flash file is launched,
variable s1 makes oval_s1 green
variable s2 makes oval_s2 yellow
variable s3 makes oval_s3 green
variable s4 makes oval_s4 red.
Robin Nicholl
6/9/2004 7:38:08 PM
On 09/06/04 7:20 pm, in article ca7kdi$6cc$1@forums.macromedia.com,
[quoted text, click to view]

First of all, your oval will need to be a named instance of an object
(movieclip). If it isn't already, select it on stage and choose 'Convert to
symbol' from the 'Modify' menu. Make sure you select the movieclip checkbox
in the dialogue that opens, and give the new movieclip a name.

Now you have to name the instance of that movieclip that you have on stage
(different to naming the movieclip itself). Select the movieclip instance on
the stage and enter a name for it in the <Instance Name> field of the
Property Inspector. Let's say you call it 'mcOval'.

Paste the function below into a new, empty frame in your movie.

_global.changeColor = function(obj, newcol) {
obj.myColor = new Color(obj);
obj.myColor.setRGB("0x" + newcol);
};

To use it to change the color of an object use this syntax:

changeColor(mcOval, "FF0000");

This will change the oval to red. So, if you wanted to use variables to set
the color you might write something like:

if (s1=="green") {
changeColor(mcOval, "4FAF00"); // this is the hex for a bright green
}

Hope that helps.

--
Robin Nicholl
studioalias
Larry.russell
6/9/2004 7:38:17 PM
I know that the variables are coming in because I have a dynamic text field
that is linked to the s1 variable and it's working right. I have a instance of
a movie symbol named mcOval and This is the code that I have on the first frame
of the movie:

loadVariables("scorecard.txt", "_root");
_global.changeColor = function(obj, newcol) {
obj.myColor = new Color(obj);
obj.myColor.setRGB("0x"+newcol);
};
if (s1 == "green") {
changeColor(mcOval, "4FAF00");
// this is the hex for a bright green
}
silvernapalm
6/9/2004 7:42:40 PM
instead of loadVariables("scorecard.txt", "_root");
try
lv = new LoadVars();
lv.load("scorecard.txt");
AddThis Social Bookmark Button