5 Replies Latest reply on Oct 16, 2011 3:45 PM by kglad

    2D Arrays causing low framerate

    Untrihexium Level 1

      I'm getting some performance issues involving tile-based maps generated with 2D Arrays. I have 5 levels in the game, but as I move back and forth across these levels (for example, when I go from level 1 to level 5 and then level 5 back to 1) the framerate drops. The more I change levels, the more lag I get.

      It has something to do with the arrays, I'm sure of that, but I don't understand what exactly causes it and how to stop it. I've tried to fix it myself but I wasn't fully successful. The lag persisted. I've searched online for similar problems but none of the solutions I've tried worked for me. I'll appreciate any guidance or suggestions as to what's going on...

       

                      Here's my code (this is all on one frame in the main timeline):

       

      levelProgress = 1;           //Level one

      _global.bricks = [];          //The array to hold all the tiles in my levels, but NOT the levels themselves
      updateLevel();                 //Calls the function that updates the level depending on levelProgress

       

      function updateLevel(){
      unloadLevel();
      switch (levelProgress){     //Switch checks what levelProgress is on

       

      case 1:
      level = new Array();          //This is the array that holds the levels; only appears in case 1
      _root.createEmptyMovieClip("lev", 1);     //Creates a movieclip to place the tiles into; only appears in case 1

       

      //Level code goes here; each level is 14 by 48. I'm only putting the first and last lines,

      level[0] = new Array(2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

      level[14] = new Array(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3);

       

       

      break;

       

      case 2:

       

      //Level code goes here, the same format as level 1 but with different numbers.

      level[0] = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

      level[14] = new Array(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3);

       

       

      break;

      //The levels go on until case 5.

      } //end of switch statement

       

      for (y=0; y<=14; y++) {         
      for (x=0; x<=48; x++) {
      if (level[y][x] != 0) {                   //Goes through the array places a tile whenever it's not at zero
      place_brick = lev.attachMovie("tile", "tile_"+lev.getNextHighestDepth(), lev.getNextHighestDepth(), {_x:x*35-15, _y:y*35});    
      place_brick.gotoAndStop(level[y][x]);
      bricks.push(place_brick);         //pushes all tiles into an array so I can delete them all later (when changing levels)
      }


      }
      }

       

      function onEnterFrame(){

      // I have code here tracking the player's _x position; when he goes to the far right or far left of the screen I do a levelProgress++ or levelProgress-- and call updateLevel()

      //Levels are all on the same frame. Whenever the player goes to another level, I update the level and move his _x position

      }

       

       

      function unloadLevel(){     //this function is called everytime the level is updated to remove the old level
      for (var i in _global.bricks){
      _global.bricks[i].removeMovieClip();      //this removes each tile. It reduces the lag slightly, but it still lags a lot!
      }
      }

        • 1. Re: 2D Arrays causing low framerate
          kglad Adobe Community Professional & MVP

          you're creating almost 700 movieclips without enabling cacheAsBitmap.  you can expect substantial lag unless you change that. 

           

          also, if you're noticing increasing lag with level changes, you're failing to remove/reset something.

          1 person found this helpful
          • 2. Re: 2D Arrays causing low framerate
            Untrihexium Level 1

            I realize that theres something I'm not properly removing, but the problem is that I don't know what. What exactly is it in my array/level that doesn't get removed? I thought it was simply the tile movieclips, but I removed those and it still lags.

             

            cacheAsBitmap helped but only made it so it took longer level changes before FPS dropped. There's still a problem: there's something I need to remove but I can't figure out what it is.

            • 3. Re: 2D Arrays causing low framerate
              kglad Adobe Community Professional & MVP

              look for loops that are not terminated (like enterframe and setInterval) and movieclips that are not removed (using for-loops iterating through your parent movieclips).

              • 4. Re: 2D Arrays causing low framerate
                Untrihexium Level 1

                Thanks for the suggestions, but all my  movieclips were removed. I've just discovered what I needed to do. I wasn't deleting the data in the bricks array I had, I was just removing the movieclips of the tiles. The actual data in the array was still there, so I added a line of code to my unloadLevel function:

                 

                function unloadLevel(){

                 

                for (var i in _global.bricks){

                _global.bricks[i].removeMovieClip();

                }

                 

                _global.bricks = [];                  

                 

                }

                 

                Clearing my arrays seemed to solve the problem, but I don't understand why it causes lag in the first place. Anyway, it works out fine now.

                • 5. Re: 2D Arrays causing low framerate
                  kglad Adobe Community Professional & MVP

                  if you weren't resetting your array, you were adding more and more references to it.  they were null references after the movieclips were removed but they still caused your array to increase in length and that increases the length of time to iterate through your array in your for-in-loop.

                  1 person found this helpful