9 Replies Latest reply on Jun 21, 2009 3:17 PM by kglad

    Simple Actionscript Looping Problem

    chester709

      I want to use a loop to generate a number of boxes.  The following code is the test loop code - set to loop just once at this stage.  The problem is that it doesn't seem to like my [i] substitutions in the 'clickBox_inst[i]'.  If I replace '[i]' with 1 it works.  I've looked at lots of example code and can't see the syntax error I must be making (it's the first look I've tried to write).  Anyone any ideas? Many thanks

       

      for (var i = 1; i < 2; i++) {

       

       

      clickBox_inst.duplicateMovieClip(["clickBox_inst"+i], 1);

      clickBox_inst[i]._x = topLeft_x + outLineWidth;

      clickBox_inst[i]._y = topLeft_y + outLineWidth;

      clickBox_inst[i]._height = iconSize;

      clickBox_inst[i]._width = iconSize;

       

      };

       

      PS - I just accidently posted this on the Flash section so apologies if anyone has read it twice.

        • 1. Re: Simple Actionscript Looping Problem
          kglad Adobe Community Professional & MVP
          try:

           

          for (var i = 1; i < 2; i++) {

           

           

          clickBox_inst.duplicateMovieClip(["clickBox_inst"+i], 1);

          this["clickBox_inst"+i]._x = topLeft_x + outLineWidth;

          etc

           

          };


          • 2. Re: Simple Actionscript Looping Problem
            chester709 Level 1

            That works perfectly - many thanks.  But I don't understand why my original script didn't work - it looked to me liked it followed the example scripts I looked at exactly!!

            • 3. Re: Simple Actionscript Looping Problem
              kglad Adobe Community Professional & MVP

              your code could only have meaning if clickBox_inst was an array.  you can then reference the ith element using clickBox_inst[i].

               

              but clickBox_inst is a movieclip, not a an array.  and if it were an array, then the duplicateMovieClip() method couldn't be applied to it.

               

              so, it looks like you are confusing two very different things.  they do both (objects and arrays) share the utilization of square brackets:  [ and ]

              • 4. Re: Simple Actionscript Looping Problem
                chester709 Level 1

                Thanks - I get the idea of what you are saying (I'm just learning though).  Continuing with what I was doing, I wasn't able to include the following line (which contains a function) in the loop following your syntax:

                 

                this["frameBox_inst"+i].onRollOver=function()

                {[frameColor"+i].setRGB(frameColorOverVar);

                };

                 

                I tried all the variations I could think off.  It doesn't really matter in this case because I can duplicate this simple line the 30 times or so - one for each box I want to create - but it would be good to include it in the look if I can.

                 

                Thanks for your help - much appreciated.

                • 5. Re: Simple Actionscript Looping Problem
                  kglad Adobe Community Professional & MVP

                  i'm not sure where frameColor1 etc is located, but if that's a child of the roll-over object, use:

                   


                   

                  this["frameBox_inst"+i].onRollOver=function()

                  {

                  this[frameColor"+i].setRGB(frameColorOverVar);

                  };

                   

                  if it's a child of the current timeline use:

                  this["frameBox_inst"+i].onRollOver=function()

                  {

                  this._parent[frameColor"+i].setRGB(frameColorOverVar);

                  };


                  • 6. Re: Simple Actionscript Looping Problem
                    chester709 Level 1

                    I'm afraid that didn't seem to work.

                     

                    I'm possibly a bit out of my depth but my code works apart from doing the last bit with a loop (which would make it very nice).  I've commented out the first few non loop lines of my solution to the loop part not working.  I'm solved the problem with the non loop so not too much to worry about but if you are interested here is my code so you might be able to spot who 'frameColor' belongs to (It's simply creating a matrix of boxes to put image icons in):

                     

                    var iconSize = 50

                    var outLineWidth = 1

                    var topLeft_x = 100

                    var topLeft_y = 50

                    var iconGap=2

                    var numberOfImages=36

                    var columns=4

                    var rows=numberOfImages/columns

                     

                    frameColorVar = String("0x" + "bbbbbb");

                    frameColorOverVar = String("0x" + "333333");

                    clickColorVar = String("0x" + "ffffff");

                     

                     

                    for (var j = 1; j < (rows+1); j++) {

                     

                    for (var i = 1; i < (columns+1); i++) { 

                     

                    frameBox_inst.duplicateMovieClip(["frameBox_inst"+(i+(j-1)*columns)], 0+i+(j-1)*columns);

                     

                    this["frameBox_inst"+(i+(j-1)*columns)]._x = topLeft_x + ((i-1)*(iconSize+2*outLineWidth+iconGap));

                    this["frameBox_inst"+(i+(j-1)*columns)]._y = topLeft_y + ((j-1)*(iconSize+2*outLineWidth+iconGap));

                    this["frameBox_inst"+(i+(j-1)*columns)]._height = iconSize + 2*outLineWidth;

                    this["frameBox_inst"+(i+(j-1)*columns)]._width = iconSize + 2*outLineWidth;

                    trace(i+(j-1)*columns);

                    clickBox_inst.duplicateMovieClip(["clickBox_inst"+(i+(j-1)*columns)], 80+i+(j-1)*columns);

                    this["clickBox_inst"+(i+(j-1)*columns)]._x = topLeft_x + outLineWidth + ((i-1)*(iconSize+2*outLineWidth+iconGap));

                    this["clickBox_inst"+(i+(j-1)*columns)]._y = topLeft_y + outLineWidth + ((j-1)*(iconSize+2*outLineWidth+iconGap));

                    this["clickBox_inst"+(i+(j-1)*columns)]._height = iconSize;

                    this["clickBox_inst"+(i+(j-1)*columns)]._width = iconSize;

                     

                    this["frameColor"+(i+(j-1)*columns)] = new Color(["frameBox_inst"+(i+(j-1)*columns)]);

                    this["frameColor"+(i+(j-1)*columns)].setRGB(frameColorVar);

                     

                    this["clickColor"+(i+(j-1)*columns)] = new Color(["clickBox_inst"+(i+(j-1)*columns)]);

                    this["clickColor"+(i+(j-1)*columns)].setRGB(clickColorVar);

                     

                    };

                    };

                     

                    for (var k = 1; k < (numberOfImages+1); k++) {

                     

                    this["frameBox_inst"+k].onRollOver=function(){

                    this._parent["frameColor"+k].setRGB(frameColorOverVar);};

                     

                    this["frameBox_inst"+k].onRollOut=function(){

                    this._parent["frameColor"+k].setRGB(frameColorVar);};

                    }

                     

                    /*

                    frameBox_inst2.onRollOver=function(){frameColor2.setRGB(frameColorOverVar);}; frameBox_inst2.onRollOut=function(){frameColor2.setRGB(frameColorVar);};

                    frameBox_inst3.onRollOver=function(){frameColor3.setRGB(frameColorOverVar);}; frameBox_inst3.onRollOut=function(){frameColor3.setRGB(frameColorVar);};

                    frameBox_inst4.onRollOver=function(){frameColor4.setRGB(frameColorOverVar);}; frameBox_inst4.onRollOut=function(){frameColor4.setRGB(frameColorVar);};

                    frameBox_inst5.onRollOver=function(){frameColor5.setRGB(frameColorOverVar);}; frameBox_inst5.onRollOut=function(){frameColor5.setRGB(frameColorVar);};

                    • 7. Re: Simple Actionscript Looping Problem
                      kglad Adobe Community Professional & MVP

                      as i mentioned, the first snippet would work if the frameColor movieclips were children of the rolled-over object and the 2nd snippet would work if the frameColor moviecips were children of the current timeline.  if neither work, you can infer that those movieclips are children of some other movieclip.

                       

                      you can alwasy use trace(frameColor0) on the timeline that contains the frameColor movieclips and use trace(this) on the frame that contains your code to find the correct reference.

                      • 8. Re: Simple Actionscript Looping Problem
                        chester709 Level 1

                        Thanks for all your help - I need to get my head around this ownership thing!

                        • 9. Re: Simple Actionscript Looping Problem
                          kglad Adobe Community Professional & MVP

                          you're welcome.