6 Replies Latest reply on Nov 28, 2006 7:36 AM by the real POTMO

    For loop variables

    ChromeShark
      Perhaps the easiest way to describe my problem is to say how iIwould write it in Flash Actionscript:

      for (lvl=1; lvl<=15; lvl++) {
      _root["locationVisited"+lvl] = false
      }

      In Lingo I would use 'repeat with', this is where I am so far:

      repeat with lvl = 1 to 15

      global locationVisited+lvl

      locationVisited+lvl = false

      end repeat

      But of course the ' locationVisited+lvl' bit is not correct, how should this be written?
        • 1. Re: For loop variables
          Level 7
          True, if I knew ActionScript but if you don't get a good response describe
          what you're trying to do inside the loop in English and I'm sure I can give
          you a Lingo answer.

          Craig


          --
          Craig Wollman
          Lingo Specialist
          Word of Mouth Productions
          212-928-9581

          www.wordofmouthpros.com
          "ChromeShark" <webforumsuser@macromedia.com> wrote in message
          news:ekcf52$ehv$1@forums.macromedia.com...
          > Perhaps the easiest way to describe my problem is to say how iIwould write
          > it
          > in Flash Actionscript:
          >
          > for (lvl=1; lvl<=15; lvl++) {
          > _root["locationVisited"+lvl] = false
          > }
          >
          > In Lingo I would use 'repeat with', this is where I am so far:
          >
          > repeat with lvl = 1 to 15
          >
          > global locationVisited+lvl
          >
          > locationVisited+lvl = false
          >
          > end repeat
          >
          > But of course the ' locationVisited+lvl' bit is not correct, how should
          > this
          > be written?
          >


          • 2. Re: For loop variables
            Level 7
            Lingo and Actionscript are completely different languages and don't
            share much in the way of sytax or functionality.

            Using a for loop to concetenate the name of a variable in Flash is a
            common practice. While it is possible in Lingo, its not commonly done.

            The more common practice is to place the names of the variables in to a
            list, this is similar to an array in Actionscript. Then operate on each
            item in the list.

            I can't tell from your example how you've named the variables but it
            should be pretty simple to just make a list and add each item.

            --
            Rob
            _______
            Rob Dillon
            Adobe Community Expert
            http://www.ddg-designs.com
            412-243-9119

            http://www.macromedia.com/software/trial/
            • 3. Re: For loop variables
              ChromeShark Level 1
              Ok then I'll explain a different way. I could list all my variables out like this:

              global locationVisited1, locationVisited2, locationVisited3, locationVisited4 ... , locationVisited15

              locationVisited1 = false
              locationVisited2 = false
              locationVisited3 = false
              locationVisited4 = false
              ....
              locationVisited15 = false

              But when working in Flash I'd sooner gauge my eyes out with a spork than list so many variables out like that, I'd just use a for loop.

              I would like to know if such a thing is possible in Lingo, and if so how I would go about writing it.
              • 4. Re: For loop variables
                Level 7
                OK, well, here is the thing... you are not using Flash. If you try
                applying Flash logic to Director, you will quickly become upset, because
                Director<>Flash. That method of declaring variables, while possible in
                Director, would not be a good idea. Director does not use Flash logic.
                It has its own way of thinking and you need to think its way for
                things to make sense. Just as when I use Flash, I have to get my brain
                out of Director mode and into Flash mode. And equally, if you are going
                to use Director, then you need to move your brain out of Flash mode.
                Trying to solve a problem Flash-wise will result in you hating
                Director. Learn to think Director-wise and you'll become a very good
                programmer.

                In Director, this sort of thing can best be done with lists. Lists are
                very powerful and quite easy to use (check the help file for linear and
                property lists, with commands like add, delete, addAt, count, etc).
                Let's make a list named locationVisited:

                global locationVisited
                locationVisited=[]

                Now all that we have is a simple global variable that has that name that
                Director knows is a list. You can add items to the group like this:

                locationVisited[1]=0

                Note that in Director, the boolean literal FALSE can be inputted as 0,
                and TRUE can be inputted as 1. You could just as easily enter:

                locationVisited[1]=FALSE

                Both do the exact same thing, but the 1st option involves less typing,
                so is preferable to me. I'm lazy that way.

                Anyway, if you wanted the 1st 15 entries in a list to be FALSE, then
                this would work:

                ---------
                --as an unwritten rule, not enforced, but just a real good idea,
                --always use the prefix g to identify global variables
                global gLocationVisited

                --this will instantiate the list if it has not already been created
                if voidP(gLocationVisited) then gLocationVisited=[]

                --now lets add 15 items to the list, set to FALSE
                repeat with lvl=1 to 15
                gLocationVisited[lvl]=0
                end repeat
                -----------

                That will make a list with 15 items in it, all of which are set to 0 (or
                FALSE).

                put gLocationVisited
                -- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]


                Now, any time you want to change one of them to TRUE, you can do this:

                gLocationVisited[1]=1

                The number in parenthesis is what you were referring to as 'lvl'. It is
                almost exactly like you used to refer to the variables, apart from the
                parentheses around the number. In memory, it is a single variable, that
                stores 15 different settings. To see what the new list looks like:

                put gLocationVisited
                -- [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

                That was the "hard way"- the Flash way if you please. There is a nice
                feature of Director lists where if you declare any item that has not yet
                been added, it will automatically "fill in the blanks" with zeros. So
                basically, this command will do the same thing as the above list of
                commands:

                global gLocationVisited
                gLocationVisited=[]
                gLocationVisited[15]=0

                That's it. To see the list it created:

                put gLocationVisited
                -- [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

                to find out what a given lvl contains, use this:

                put gLocationVisited[5]
                -- 0

                gLocationVisited[5]=1

                put gLocationVisited
                -- [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

                put gLocationVisited[5]
                -- 1

                If you need to add more than the original 15, you can easily add them
                using the add() commmand (or just declare gLocationVisited[16]=0). You
                will find, once you get used to working with Director lists, that they
                work very well, and extremely quickly. It is a different way of thinking
                about data than Flash uses, but it works very well.
                • 5. Re: For loop variables
                  duckets Level 1
                  Just FYI, flash can also use lists (arrays), so it's not really a case of flash logic being different to director logic. Just that the original poster doesn't appear to familiar with the use of arrays in flash! :-)

                  - Ben
                  • 6. Re: For loop variables
                    the real POTMO Level 1
                    anyway if you'd like to define variables in a loop (its not the best way to do anything but you CAN do it)
                    use the do command. The DO command compiles and executes a string of LINGO. I Dont recomend you working like this (see previous posts about arrays and lists)

                    repeat with i = 1 to 100
                    do("variable" & string(i) && "=" & string(i+3))
                    end repeat

                    put variable1 -- 4
                    put variable6 -- 9


                    /POTMO.com