• Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
    Dedicated community for Japanese speakers
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
    Dedicated community for Korean speakers
Exit
0

Turning if statement into variable?

Community Beginner ,
Jun 22, 2012 Jun 22, 2012

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?

TOPICS
ActionScript

Views

768

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Jun 22, 2012 Jun 22, 2012

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"));

Votes

Translate

Translate
Enthusiast ,
Jun 22, 2012 Jun 22, 2012

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 22, 2012 Jun 22, 2012

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jun 22, 2012 Jun 22, 2012

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"));

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 22, 2012 Jun 22, 2012

Copy link to clipboard

Copied

Thank you for the response, esdebon. This concept will come in handy in the future.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jun 22, 2012 Jun 22, 2012

Copy link to clipboard

Copied

LATEST

you're welcome

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines