Flash Game Development with Flex and Actionscript - Defining a Level (Page 3 of 3)

Article by Matthew Casperson (4,880 pts ) , published Nov 6, 2009

Level.as

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 static const TimeBetweenClouds:Number = 2.5;

protected static const TimeToLevelEnd:Number = 2;

protected var nextDefinitions:Array = null;

protected var levelID:int = 0;

protected var totalTime:Number = 0;

protected var timeToNextLevelElement:Number = 0;

protected var levelElementGraphics:ArrayCollection = new ArrayCollection();

protected var timeToNextCloud:Number = 0;

protected var timeToLevelEnd:Number = 0;

protected var backgroundMusic:SoundChannel = null;

public var levelEnd:Boolean = false;

static public function get Instance():Level

{

if ( instance == null )

instance = new Level();

return instance;

}

public function Level()

{

levelElementGraphics.addItem(ResourceManager. SmallIslandGraphics);

levelElementGraphics.addItem(ResourceManager. BigIslandGraphics);

levelElementGraphics.addItem(ResourceManager. VolcanoIslandGraphics);

}

public function startup(levelID:int):void

{

timeToNextLevelElement = 0;

new Player().startupPlayer();

timeToLevelEnd = TimeToLevelEnd;

levelEnd = false;

backgroundMusic = ResourceManager.Track1FX.play(0, int.MAX_VALUE);

this.totalTime = 0;

this.levelID = levelID;

nextDefinitions = LevelDefinitions.Instance. getNextLevelDefinitionElements(levelID, 0);

}

public function shutdown():void

{

backgroundMusic.stop();

backgroundMusic = null;

}

public function enterFrame(dt:Number):void

{

totalTime += dt;

if (nextDefinitions == null)

{

if (Enemy.pool.NumberOfActiveObjects == 0)

levelEnd = true;

}

else

{

var nextLevelDefTime:Number = (nextDefinitions[0] as LevelDefinitionElement).time;

if (totalTime >= nextLevelDefTime)

{

for each (var levelDefElement:LevelDefinitionElement in nextDefinitions)

levelDefElement.func();

nextDefinitions = LevelDefinitions.Instance.getNextLevelDefinitionElements(levelID, nextLevelDefTime);

}

}

// add a background element

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);

}

// add cloud

timeToNextCloud -= dt;

if (timeToNextCloud <= dt)

{

timeToNextCloud = TimeBetweenClouds;

var cloudBackgroundLevelElement:BackgroundLevelElement = BackgroundLevelElement.pool.ItemFromPool as BackgroundLevelElement;

cloudBackgroundLevelElement.startupBackgroundLevelElement(

ResourceManager.CloudGraphics,

new Point(Math.random() * Application.application.width, -ResourceManager.CloudGraphics.bitmap.height),

ZOrders.CloudsBelowZOrder,

75);

}

if (levelEnd)

{

timeToLevelEnd -= dt;

var scale:Number = timeToLevelEnd / TimeToLevelEnd;

if (scale < 0) scale = 0;

var transform:SoundTransform = backgroundMusic.soundTransform;

transform.volume = scale;

backgroundMusic.soundTransform = transform;

}

if (timeToLevelEnd <= 0)

Application.application.currentState = "LevelEnd";

}

}

}

We have removed all the code and properties relating to the random creation of enemies. For now we will leave in the creation of random BackgroundElements, although eventually the entire level will be defined inside the LevelDefinitions class.

There are 3 new properties added: nextDefinitions, levelID and totalTime. The nextDefinitions property contains an array of LevelDefinitionElements which are to be execute next. In the enterFrame function the Level class requests the next batch of LevelDefinitionElements through the LevelDefinitions getNextLevelDefinitionElements function. Then, when the time is right, these LevelDefinitionElements are executed and the next bacth requested. When there are no more LevelDefinitionElements (i.e. nextDefinitions == null) the Level waits for all the Enemies to be destroyed and then ends the level (by setting levelEnd to true).

The levelID property simply defines the current level. It is the same value supplied to the addLevelDefinition function.

The totalTime property represents the total time the current level has been active for. It is used in conjunction with nextDefinitions to execute the next batch of LevelDefinitionElements at the appropriate time.

By defining a level structure as a sequence of functions that get executed at predetermined points in the game we have the ability to do some pretty cool things down the track.

Go back to Flash Game Development with Flex and ActionScript

Related Files

Showing page 3 of 3
 
Subscribe to Web Development
RSS
Get free weekly updates, directly to your inbox.
Browse Web Development