6 Replies Latest reply: Jul 19, 2012 9:03 AM by witherton RSS

    Possible:Booleans in Arrays?

    witherton

      I would like to use an array to A)define the Boolean state of a group of variables and B) check those variables. I have tried a bunch of different methods of doing this but get a bunch of different errors. Is this actually something that's possible? What would sample code look like?

        • 1. Re: Possible:Booleans in Arrays?
          _spoboyle Community Member

          definately possible, several different ways of doing no different to anything else really

           

          post code and errors for specific help

           

          var i:int = -1

           

           

          var boolArray:Array = [true, false, 3>2, true, false, false, i == -2, true];

          trace("4th bool: " + boolArray[3])

           

           

          if (boolArray[2])

          {

                    trace("3rd bool is true");

          }

          else

          {

                    trace("3rd bool is false");

          }

          • 2. Re: Possible:Booleans in Arrays?
            witherton

            Let's say I have this array:

             

            var buttons:Array=[button1, button2,button3]

             

            Instead of doing this for each index:

            var button1:Boolean=false

             

            I would like to do something like this:

            buttons[i]:Boolean=false

             

            Then I would like to be able to do something like this for conditional:

            if (buttons[i]=true){trace ("true")}

            • 3. Re: Possible:Booleans in Arrays?
              _spoboyle Community Member

              then you can just use buttons[i] = false;

               

              and

               

              if (buttons[i] == true) {trace("true")}

               

              (note using single = is an assigment not an equality)

               

              here is another demo that might help

               

              package

              {

                        import flash.display.Sprite;

               

                        public class Main extends Sprite

                        {

                                  private var buttons:Array;

               

                                  public function Main()

                                  {

                                            buttons = new Array();

               

                                            setUpButtons(6);

               

                                            testButtons();

                                  }

               

                                  private function testButtons():void

                                  {

                                            for (var i:int = 0; i < buttons.length; i++)

                                            {

                                                      if (buttons[i])

                                                      {

                                                                trace("button " + i + " is true");

                                                      }

                                                      else

                                                      {

                                                                trace("button " + i + " is false");

                                                      }

                                            }

                                  }

               

                                  private function setUpButtons(len:int):void

                                  {

                                            for (var i:int = 0; i < len; i++)

                                            {

                                                      buttons[i] = false;

                                            }

                                  }

                        }

              }

              • 4. Re: Possible:Booleans in Arrays?
                witherton

                Ok, I've been playing around with this and it looks like you have to define the contents of the array as a variable first, then define the contents as a variable.

                • 5. Re: Possible:Booleans in Arrays?
                  _spoboyle Community Member

                  that will work but is not the case if you want to paste your code here I can get it to work without the variables first

                  • 6. Re: Possible:Booleans in Arrays?
                    witherton

                    Thanks. This is the code:

                     

                    var button1, button2,button3;

                    var buttons:Array=[button1, button2,button3];

                    for (var i:uint=0;i<buttons.length; i++)

                    buttons[i]=false;

                    for (var j:uint=0;j<buttons.length; j++)

                    if (buttons[j]!=true){trace([j])}