In order to have progress bar you will have to divide level loading process into smaller steps. There is a number ways to do it. In my case instead of initializing world in contructor I have added extra method called initProgress(progress:uint)
that initializes the world in several steps. Here’s some code:
package
{
public class Game extends World
{
public function initProgress(progress:uint, map:String, addPlayer:Boolean = false):void
{
switch (progress)
{
case 0:
// load level
break;
case 1:
// load actors
break;
case 2:
// do sth else
break;
//...
case 8: // here we assume that there are 9 steps to init this world (0-8)
// finalize
updateLists();
default:
}
}
}
}
package
{
public class Main extends Engine
{
private var progressInit:uint = 0;
public function postInitProgress():Number
{
switch (progressInit)
{
case 0:
worlds = new Object();
break;
case 20:
case 21:
case 22:
case 23:
case 24:
case 25:
case 26:
case 27:
case 28:
wn = "caves";
if (!worlds[wn]) worlds[wn] = new Game(null, wn);
Game(lastWorld).initProgress(progressInit - 20, "output15.xml");
break;
case 30:
case 31:
case 32:
case 33:
case 34:
case 35:
case 36:
case 37:
case 38:
wn = "chamber";
if (!worlds[wn]) worlds[wn] = new Game(null, wn);
Game(lastWorld).initProgress(progressInit - 30, "chamber04.xml", true);
break;
default:
}
progressInit++;
return progressInit / 38; // final step
}
}
}
The second part may be a little confusing but the basic idea is to run Main’s postInitProgress() in a loop till it returns number bigger that 1. The returned value may be used for progress indicator.