Skip to main content
Inspiring
May 5, 2014
Question

Need help speeding up a loop that spawns a virtual world.

  • May 5, 2014
  • 1 reply
  • 261 views

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.

This topic has been closed for replies.

1 reply

Amy Blankenship
Legend
May 5, 2014

You're not going to be able to get away with having that many objects on the Display list. Instead, you'll need to use one or more BitmapData objects that you draw() or copyPixels() or similar to.