2D Arrays causing low framerate
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
place_brick = lev.attachMovie("tile", "tile_"+lev.getNextHighestDepth(), lev.getNextHighestDepth(), {_x:x*35-15, _y:y*35});
place_brick.gotoAndStop(level
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.removeMovieClip(); //this removes each tile. It reduces the lag slightly, but it still lags a lot!
}
}