I'm trying to add elements to an ArrayList (in Processing.js, basically a javascript array) in a for loop. BEcause of the many objects I need to add, the for loop is not incrementing all the way through. I basically have an ArrayList of levels (like in a game) and each level has it's own ArrayList of GameObjects. I'm first adding all the level objects to their own array, them I'm looping through that ArrayList to "make" each level, a.k.a. add a bunch of gameObjects to that level based on a String array. Here's my code:
void setupAllLevels() {
for(int i = 0; i < map.length(); i++) {
levels.add(new Level(i));
}
for(int i = 0; i < levels.size(); i++) {
levels.get(i).makeLevel();
}
}
And here's the makeLevel function within each level object:
void makeLevel() {
for(int y = 0; y < map[levelNum].length(); y++) {
for(int x = 0; x < map[levelNum][y].length(); x++) {
if(mapKey[map[levelNum][y][x]]) {
gameObjects.add(new (mapKey[map[levelNum][y][x]])(x * tileSize, y * tileSize, levelNum));
}
}
}
}
it works when I only try to make one level, but looping through all of them and making them results in either half of a level being made, or none of a level (it's kind of random how much it works). I don't care how long this function takes, because I'm doing it in setup(). Does anyone have any ideas of how to do this process so that every level gets made? Possibly by slowing down the for loop? Ive tried setTimeout(loop here, 0), but that hasn't worked. Any ideas?