Copy link to clipboard
Copied
I have a group of movie clips on the stage that belong to an array called "bttns". All consist of the same 3 frame labels: "no_color", "red"and "blue" . When the program starts, each of the buttons is either on frame "no_color" or "red". Their currentframes are determined by a randomizer code that fires off at start. I want a second randomizer to select one button from an array of "red" buttons to turn "blue". I've tried a few different things with for loops and while loops, but none does exactly what I want in an elegant way.
When I trace the the following function, I get exactly the array I want.
{
for(var i:uint=0; i<bttns.length; i++)
if (bttns.currentLabel != "blue"){trace(bttns.name)}
}
However, I don't know how to turn this code into a variable. If I use the "!=" tag in a variable definition, it traces as true or false.Is it possible to do so?
var onlyRed:Array;
var i:uint;
onlyRed=new Array(); //Clear the array before init the for
for(i=0; i<bttns.length; i++)
if (bttns.currentLabel == "red"){ // or if (bttns.currentLabel != "blue"){
onlyRed.push(bttns);
}
}
i=int(Math.random()*(onlyRed.length-1));
bttns.gotoAndStop("blue"));
Copy link to clipboard
Copied
{
for(var i:uint=0; i<bttns.length; i++)
if (bttns.currentLabel == "red" && Math.random()>0.5){
bttns.gotoAndStop("blue"))
}
}
With this, take all the red buttons and have 50% to turn into blue
Copy link to clipboard
Copied
Thank you for the reply, Esdebon! I have tried something like this before and the problem with this type of code is that there is a chance that the code may not choose one of the red buttons. Also, I would like to select only one of the buttons red buttons during a particular turn.
It seems like there has to be a way to get the buttons that are currently "red" into an array and then randomly choose from that array. It sounds simple, but it's been giving me a headache.
Copy link to clipboard
Copied
var onlyRed:Array;
var i:uint;
onlyRed=new Array(); //Clear the array before init the for
for(i=0; i<bttns.length; i++)
if (bttns.currentLabel == "red"){ // or if (bttns.currentLabel != "blue"){
onlyRed.push(bttns);
}
}
i=int(Math.random()*(onlyRed.length-1));
bttns.gotoAndStop("blue"));
Copy link to clipboard
Copied
Thank you for the response, esdebon. This concept will come in handy in the future.
Copy link to clipboard
Copied
you're welcome