Need help speeding up a loop that spawns a virtual world.
I have a code, that uses a loop to spawn a world that is 100x100 blocks, each block is 20x20 pixels. It takes a long time to spawn these blocks, 5 - 10 seconds, I plan to increase the size of the world to 20000x1000, which will result in 20 million blocks being spawned, I do not need all of the blocks to be seen at once, only the blocks that are being shown in the UI ScrollBar atm, is there a way that I can speed up the spawning by only having like 1/100th of them visible at a time? Remember, I only need the ones visible in the ScrollBar at the time to be seen. Here is my algorithm for spawning the terrain:
private function spawnMap():void
{
for (var i:Number=1; i<=100; i++)
{//FIRST LOOP
var dirtblock:DirtBlock = new DirtBlock();
var stoneblock:StoneBlock = new StoneBlock ;
if (blockY >= 9)
{
stoneblock.x = i * 20;
stoneblock.y = blockY*20 - 20
;
blockholder.addChild(stoneblock);
listofblocks = stoneblock.x + "," + stoneblock.y;
}
else
{
dirtblock.x = i * 20;
dirtblock.y = blockY*20 - 20
;
blockholder.addChild(dirtblock);
listofblocks = dirtblock.x + "," + dirtblock.y;
}
if (i == 100 && blockY < 100)
{
blockY++;
i = 0;
gamescroll.update();
}
Maybe my computer is just slow spawning 10K blocks, or maybe I am using the wrong programming language to do this sort of thing, this app is planned to be made for mobile devices. If someone has any other technique or method to speed up this slow laggy process, please point me in the right direction.
