0 Replies Latest reply on May 23, 2008 4:17 AM by Meeeeesta

    Help me kick my for loop addiction

    Meeeeesta
      Hello.

      I'm trying to create a random grid quickly without crippling Flash to much. However my over reliance on loops causes the 256 loops error to appear (thinking I've created an infinite loop by accident). If I add a slight pause into the loop (onEnterFrame or setInterval) it works (albeit slowly). In nutshell I want to try and cut down on the number of 'condition testing' loops if possible.

      var maxWidth = 480; //horizontal area width
      var maxHeight = 320; //vertical area width
      var unitSize = 10; // this is the unit size

      var maxXcount = maxWidth / unitSize; // number of units horizontally
      var maxYcount = maxHeight / unitSize; // number of units vertically

      for (i=0; i<maxXcount; i++){
      grid = new Array();
      for (j=0; j<maxYcount; j++){
      grid
      [j] = false;
      //trace("grid["+i+"]["+j+"] = "+grid [j]);
      }
      }

      I started off by defining the max dimensions of the area and the size of the units within that area. Then looped through these to create a dual array which amounts to var gridElement [XPOS] [YPOS], doing nested loops to create this. No problems here. Every entry is set to false and used as an 'activatation' marker.

      My problem lies hereon. If I stick with a fixed size the process is easy, it's my random size generator that seems to cause the issue. To create random sized shapes within this gird (that fit exactly) I end up adding more equations to the mix. Starting at 0,0 by the grid array, I loop from the current x position through to the maximum shape width + current x pos checking true/false to see if that grid entry is being 'used' or not. A counter inside the loop is increamented, if no match is found the full random value is used, if a match is found or the random width+xpos exceeds the max area width, the loop 'breaks' and the current increament counter value is used instead.

      This process is repeated for the Y axis (mainly to test the height hasn't exceeded the max Area height).

      var xCount;
      var yCount;

      for (i=0; i<maxDimension; i++){
      xCount = i;
      if (grid[(i+gridXCount)][gridYCount] || (i+gridXCount)>=maxXcount){ // check horizontally for 'TRUES' and not bigger than the X size of grid (maxXCount);
      break;
      }
      }

      for (j=0; j<maxDimension; j++){
      yCount = j;
      if (grid[gridXCount][j+gridYCount] || (j+gridYCount)>=maxYcount){ // check vertically for 'TRUES' and not bigger than the size of Y grid (maxYCount);
      break;
      }
      }


      var xWidth = randRange(1, xCount); // Get a random size 1 - (5 or smaller depending what else is around)
      var yWidth = randRange(1, yCount); // get a random size 1 - (5 or smaller depending what else is around)

      After the loops are finished, I now have my random shape which will not write over an existing shape and will not spill out of the main grid area. So now I do one last loop to make all the grid entries which fall inside this shape are set to true so they don't get reused.

      for (j=gridYCount; j<(gridYCount+yWidth); j++){ //Set the Grid to true
      for (i=gridXCount; i<(gridXCount+xWidth); i++){
      grid
      [j] = true;
      }
      }

      Then I jump the main X count on by the width of the new shape and start the process again test whether the current X,Y grid entry is true or false, if it is false it does the Maths again, if true the Xcounter jumps on 1 and tries again.

      This system does work, but because I have to introduce a delay in the looping it causes quite a delay at the start when the swf is played. I just wondered if there's a less loop intensive way so I could also do away with the delay between each loop.

      I know this is alot to ask, but any help or pointers on this matter would be greatly appreciated.

      Many thanks,
      Meeeeesta