Random color (setRGB)

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…

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:

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

woohoo! It works! You guys are great!
That prototype thingy is pretty neat! :slight_smile:

And I noticed that I don’t have to put
movieToChange*.setRandomColor();*
if the movie is defined in the function.