package
{
import flash.events.*;
import flash.geom.*;
import flash.media.*;
import flash.net.*;
import flash.utils.*;
import mx.collections.ArrayCollection;
import mx.core.*;
public class Level
{
protected static var instance:Level = null;
protected static const TimeBetweenLevelElements:Number = 2;
protected var timeToNextLevelElement:Number = 0;
protected var levelElementGraphics:ArrayCollection = new ArrayCollection();
static public function get Instance():Level
{
if ( instance == null )
instance = new Level();
return instance;
}
public function Level(caller:Function = null )
{
if ( Level.instance != null )
throw new Error( "Only one Singleton instance should be instantiated" );
levelElementGraphics.addItem(ResourceManager.SmallIslandGraphics);
levelElementGraphics.addItem(ResourceManager.BigIslandGraphics);
levelElementGraphics.addItem(ResourceManager.VolcanoIslandGraphics);
}
public function startup():void
{
timeToNextLevelElement = 0;
new Player().startupPlayer();
}
public function shutdown():void
{
}
public function enterFrame(dt:Number):void
{
timeToNextLevelElement -= dt;
if (timeToNextLevelElement <= 0)
{
timeToNextLevelElement = TimeBetweenLevelElements;
var graphics:GraphicsResource = levelElementGraphics.getItemAt(MathUtils.randomInteger(0, levelElementGraphics.length)) as GraphicsResource;
var backgroundLevelElement:BackgroundLevelElement = BackgroundLevelElement.pool.ItemFromPool as BackgroundLevelElement;
backgroundLevelElement.startupBackgroundLevelElement(
graphics,
new Point(Math.random() * Application.application.width, -graphics.bitmap.height),
ZOrders.BackgoundZOrder,
50);
}
}
}
}
Like the GameObjectManager, the Level class implements the Singelton design strategy with an instance property and an Instance function. The TimeBetweenLevelElements property defines how long to wait between adding new BackgroundLevelElements to the screen, while the timeToNextLevelElement property keeps a count of how long is has been since the last BackgroundLevelElement was added. The levelElementGraphics property holds a collection of GraphicResources which we will randomly select from when creating our new BackgroundLevelElements.
Like most classes the Level class has a startup and shutdown function. It is during the startup function (which is called by the Application object when we change into the Game state) that we create the player. We also have an enterFrame function. Just like the GameObject, the Levels enterFrame function is called once per frame by the GameObjectManager. It is here that we periodically pull out an unused BackgroundLevelElement from the resource pool and initialise it through the startupBackgroundLevelElement function.
By adding user input and extending the concepts introduced by the Bounce class in part 3 to create a scrolling background our we have created something that is starting to look like a game. In part 5 of the series we will add some enemies to the screen, and give the player some weapons.
Go back to Flash Game Development with Flex and ActionScript