5 Replies Latest reply on Mar 30, 2007 3:00 AM by pandxsg

    creating for loop for a boolean

    pandxsg
      Hi,

      I'm still a beginner to AS so pardon me if this may sound simple...

      I've created 16 booleans with all set to false.

      Now with a for loop, I'm trying to create 16 buttons, and when each button is pressed, the respective boolean relative to that button will change to true. Looks something as below:

      Somehow, this doesn't work...can someone point out the error in this coding? Thanks very much!


        • 1. Re: creating for loop for a boolean
          Level 7
          Couple things...

          It's standard for a button to work onRelease, not onPress.

          this - within your onPress function is referring to the button. Not the root
          timeline like you probably want to target. The real problem though, is the
          reference to i within the button. If you do a trace(i); within the onPress
          you'll find every button will trace out 17. That is because the button does
          not keep a local reference to i - all button are just referencing the final
          value of i on the main timeline... it's called scope. Sometimes scope issues
          can be a real challenge.What you need to do is create a local variable
          within the buttons and store i in that variable.

          for (var i = 1; i <= 16; i++) {
          this["button" + i].i = i;
          this["button" + i].onPress = function() {
          this._parent["status" + this.i] = true;
          }
          }

          --
          Dave -
          Head Developer
          www.blurredistinction.com
          Adobe Community Expert
          http://www.adobe.com/communities/experts/


          • 2. Re: creating for loop for a boolean
            pandxsg Level 1
            Thanks for the help, it works like a charm! Your explanation is very clear also, however for one part I do not understand:

            this._parent["status" + this.i] = true;

            What is the purpose of "_parent" here? Will this["status" + this.i] = true; not work?
            • 3. Re: creating for loop for a boolean
              pandxsg Level 1
              Sorry I've another question here:

              How do I set the other booleans to false, after I've changed one to true? For example, if I changed "status5" to true, how do I set "status1-4" and "status6-16" to false?

              Thanks!
              • 4. Re: creating for loop for a boolean
                Level 7
                >> What is the purpose of "_parent" here? Will this["status" + this.i] =
                >> true;
                not work?

                Did you not try it? As I mentioned, 'this' within your onPress function
                (which is attached to each button) refers to the timeline of the button.
                Literally - 'this button'. If you do a: trace(this); within the onPress
                you'll see the button's instance traced... so if you want to refer to a
                variable on a different timeline, root in this case, saying 'this' won't
                work - because you're referring to the button's timeline... and your
                variables are on the root timeline. Does that make sense? If you keep in
                mind that the main timeline in Flash is just another movie clip, it helps to
                see the hierarchy better.
                Because the button is placed 'inside' the root timeline, it is a 'child' of
                that timeline - and thus stating this._parent gets you back to root... Just
                as referring to the .. directory gets you to the parent directory on a hard
                drive. You could of course just say _root["status" + this.i] - but it's
                generally not a good idea to explicitly refer to root.

                >> How do I set the other booleans to false, after I've changed one to true?
                >> For
                example, if I changed "status5" to true, how do I set "status1-4" and
                "status6-16" to false?

                Call a function, and pass in the index of the button that was pressed.
                Within the called function, iterate through your buttons and set the
                variable to false for all but the index that was passed in:

                Instead of:

                this["button" + i].onPress = function() {
                this._parent["status" + this.i] = true;
                }

                You'd call a function:

                this["button" + i].onPress = function() {
                this._parent.btnPressed(this.i);
                }


                And on your root, you'd have the btnPressed function:

                function btnPressed(btnIndex:Number) {
                for (var i = 1; i <= 16; i++) {
                this["status" + i] = false;
                }
                this["status" + btnIndex] = true;
                }


                First, just run through and set all the status'n' variables to false, and
                then set the one to true


                --
                Dave -
                Head Developer
                www.blurredistinction.com
                Adobe Community Expert
                http://www.adobe.com/communities/experts/


                • 5. Re: creating for loop for a boolean
                  pandxsg Level 1
                  I see the picture now...thanks very much!