8 Replies Latest reply on Nov 7, 2007 8:01 PM by kglad

    For loops

    JoeLongstreet
      I'm new to actionscript and I'm trying to understand the for loop. I have this chunk of code which does what I want it to do but I don't really understand why it works. From looking at it I would think it would put the boxes in the same exact spot, but it doesn't?

        • 1. Re: For loops
          kglad Adobe Community Professional & MVP
          the += means you increment boxX by 50 on each iteration.

          the -= means you decrement boxY by 50 on each iteration.
          • 2. Re: For loops
            Level 7
            JoeLongstreet,

            > I have this chunk of code which does what I want it
            > to do but I don't really understand why it works.

            Let's step through it. :)

            > From looking at it I would think it would put the boxes
            > in the same exact spot, but it doesn't?

            Let's see what it does.

            var box:yellowBox;

            So far, you've declared a variable named box, which an instance of the
            yellowBox class (presumably some custom class you've written, or a Linkage
            Class -- what used to be called a Linkage identifier -- in your Library).

            > var boxX:Number = 100;
            > var boxY:Number = 300;

            Here, you've declared two more variables, boxX and boxY, which are both
            instances of the Number class, set to 100 and 300 respectively.

            for(var i:Number = 0; i < 6; i++) {

            And here's that for() loop. Inside the parentheses, you have three
            parts:

            a) var i:Number = 0

            That sets up your counter. It's just another variable, again a number,
            and this time starting with zero. The variable i is going to count from
            zero to some other number to be determined in a moment.

            b) i < 6

            This is the condition. This particular loop is going to count until the
            value of i reaches 6 (specifically, the loop will continue while i is less
            than 6 ... so technically, if you set the counter to count backwards --
            0, -1, -2, -3, etc., the loop would go forever).

            c) i++

            This tells i how to increment. The ++ operator increments by 1, so the
            first time around, i is equal to zero; next time around, it's equal to 1;
            after that, it's 2, and so on. As each loop progresses, the value of i is
            compared against 6 (from step b), and as long as it's less than 5, the loop
            continues.

            box = new yellowBox();
            addChild(box);

            In these two lines, that earlier variable, box, is set to an instance of
            the yellowBox class, then added to the display list of the timeline it's in
            (presumably the main timeline). This is the first set of instructions
            inside the for() loop's curly braces.

            box.x = boxX;
            box.y = boxY;

            Here's the x and y properties of the yellowBox instance are set to
            whatever the values of boxX and boxY are. In the first run of the loop,
            their values are 100 and 300, as determined earlier.

            boxY -= 50;
            boxX += 50;

            Here, the boxX and boxY variables' values are changed. boxX is
            incremented by 50 and boxY is decremented by 50. When the loop runs its
            second lap (where i equals 1) the value of boxX will be 150 and the value of
            boxY will be 250.

            On every lap, these same last two lines increment the values of those
            variables again. On the third lap, for example, the values will be updated
            to 200 and 200; fourth lap, 250 and 150, etc.

            For fun, you can use a trace() statement to "see" these values change in
            realtime:

            var box:yellowBox;
            var boxX:Number = 100;
            var boxY:Number = 300;
            for(var i:Number = 0; i < 6; i++) {
            box = new yellowBox();
            addChild(box);
            box.x = boxX;
            box.y = boxY;
            boxX += 50;
            boxY -= 50;
            trace(boxX, boxY);
            }


            David Stiller
            Co-author, Foundation Flash CS3 for Designers
            http://tinyurl.com/2k29mj
            "Luck is the residue of good design."


            • 3. Re: For loops
              Rothrock Level 5
              The trick is the -= and the +=. Those are secret shortcuts for the following:

              boxY = boxY-50;
              boxX = boxX+50;

              So each time you go through the loop the values of the x and y are being changed. That is why they aren't all in the same spot.

              Or do you mean something else?
              • 4. Re: For loops
                JoeLongstreet Level 1
                Mr. David Stiller,

                Thanks for your help. I read through your reply and I think I actually get it now. I wasn't understanding that box.x was replacing the original value of boxX. Thanks again,

                Joe
                • 5. Re: For loops
                  Level 7
                  Joe,

                  > Thanks for your help.

                  Sure thing! Looks like kglad, Rothrock, and I all mashed heads -- like
                  Laurel and Hardy. At least we're all saying the same thing. ;)

                  > I read through your reply and I think I actually get it
                  > now. I wasn't understanding that box.x was replacing
                  > the original value of boxX.

                  That's not quite it. The thing that replaces the values of boxX and
                  boxY is the += and -= operators themselves. The x and y properties aren't
                  actually setting anything here; they're only receiving values from boxX and
                  boxY.

                  Here's another way of looking at it:

                  boxX += 50;

                  ... is the same as saying this:

                  boxX = boxX + 50;

                  ... just shorthand.


                  David Stiller
                  Contributor, How to Cheat in Flash CS3
                  http://tinyurl.com/2cp6na
                  "Luck is the residue of good design."


                  • 6. Re: For loops
                    kglad Adobe Community Professional & MVP
                    too many trees david.
                    • 7. Re: For loops
                      Level 7
                      haha At least they're digital!


                      "kglad" <webforumsuser@macromedia.com> wrote in message
                      news:fgtpeb$apc$1@forums.macromedia.com...
                      > too many trees david.


                      • 8. Re: For loops
                        kglad Adobe Community Professional & MVP
                        maybe you should put the KEY issue in caps or, in some other way, highlight it.