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…
system
July 12, 2002, 10:26am
2
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:
system
July 12, 2002, 8:46pm
3
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();
}
system
July 13, 2002, 2:58pm
4
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.