View Full Version : Random color (setRGB)
andr.in
July 12th, 2002, 01:35 AM
I'm trying to make a button that changes aa mc's color to a random one from an array.
The array:
mcarray = new Array();
mcarray = new Array(3);
mcarray = new Array("0x00FFFF", "0x00FF00", "0xFFFF00", "0x0000FF")
and the actions for a btn:
on (release) {
mccolor = new Color(_root.mc);
mccolor.setRGB(Math.random(mcarray));
}
But the mc turns black instead of some another color...
I think it's the "Math.random(mcarray)" there...
or is it something else? :-\
Any help would be appreciated...
pom
July 12th, 2002, 06:26 AM
The thing is, the Math.random () function doesn't take any parameter. It's not a method that you can apply to an array. The way to select an element of an array would be myArray[0] for the first element.
The code for your button should the be
on (release) {
myNum = Math.floor(Math.random()*3) ;
mccolor = new Color(_root.mc);
mccolor.setRGB(mcarray[myNum]);
}Hope it helps.
pom
:asian:
sbeener
July 12th, 2002, 04:46 PM
here's a shortcut for declaring your array:
mcarray = ["0x00FFFF", "0x00FF00", "0xFFFF00", "0x0000FF"];
and to make that button event more dynamic, haw about referencing the array length rather than spec'ing it statically?
on (release) {
mccolor = new Color(_root.mc);
mccolor.setRGB(mcarray[Math.floor(Math.random()*mcarray.length)]);
}
but as always, probably better to write a function or method:
MovieClip.prototype.setRandomColour = function(){
var c = new Color(this);
var a = _root.mcarray;
c.setRGB(a[Math.floor(Math.random()*a.length)]);
}
then the button would read:
on(release){
movieToChange.setRandomColour();
}
andr.in
July 13th, 2002, 10:58 AM
woohoo! It works! You guys are great!
That prototype thingy is pretty neat! :)
And I noticed that I don't have to put
movieToChange.setRandomColor();
if the movie is defined in the function.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.