6 Replies Latest reply on Mar 21, 2007 12:53 PM by kglad

    for loop conundrum

      Hi all,

      I have a problem with using a for loop which I would like your help with.

      The problem is as follows :-

      I have 10 buttons on the stage, all with instance names of "btn1", "btn2", "btn3", etc etc.....

      When a user clicks one of these buttons, I want a corresponding jpg to be opened ("1.jpg", "2.jpg" etc)

      I thought the best way to get this to work was to use a for loop to marry up all the duplicate numbers in the instance names and file names.

      I wrote the script and tested the movie, and flash detects I am pressing the buttons, but only plays one of the files, not the corresponding "1.jpg" for "btn1"

      I have attached the code im using for you to help me.

      Many thanks in advance - im intrigued as to the solution!!


        • 1. Re: for loop conundrum
          kglad Adobe Community Professional & MVP
          there are several problems with your code. the first three are:

          the onEnterFrame handler executes repeatedly. i know it "sounds" like it should execute once when the playhead enters the frame that contains your code but that's not how it works.

          for code that you want to execute once when the frame is played, just attach it to the frame. for code that you want to loop repeatedly, use the onEnterFrame handler (or some other looping function like setInterval() ).

          next, your onPress method does not reference your buttons. so, you'll probably end up defining an onPress method for the timeline that contains that code.

          next, your buttons have no way to remember or retrieve anything about the value of i when their onPress method was defined.


          1 person found this helpful
          • 2. Re: for loop conundrum

            Superb, the code works like a dream, thanks.

            I am still in the dark to why it works as yet, but im gonna figure out that right now.

            Cheers again,

            • 3. Re: for loop conundrum
              kglad Adobe Community Professional & MVP
              you're welcome.
              • 4. Re: for loop conundrum
                Hi kglad,

                I cant figure out how "this["btn"+i].ivar=i " part is working?

                To me, it looks like your referencing a MC within one of my btn MCs called ivar!! (or something along those lines of thought...)

                I take it what is happening here is you are creating some sort of variable (ivar) to store what number button was pressed, and using this to direct to the relevant jpg?

                Thanks for your time,


                • 5. Re: for loop conundrum
                  BorosAdam Level 1
                  that's correct! ;) he's putting the number of the button inside the button (this is ivar) to make the button 'remember' where to go... so, in every button there is a variable called ivar, kind of a serial number for the button.

                  1 person found this helpful
                  • 6. Re: for loop conundrum
                    kglad Adobe Community Professional & MVP
                    that's not "...referencing a MC within one of my btn MCs called ivar...". it's storing the value of i in ivar when the mouse handler of this["btn"+i] is created.

                    that's always applicable when using a loop to define a mouse handler when the mouse handler must retrieve information (like the button number) after the loop completes.

                    in your situation you could also use the flash string methods to retrieve the button in the mouse handler, but that's not always applicable.
                    1 person found this helpful