Flash Game Development with Flex and Actionscript - User Input and an Animated Background (Page 5 of 5)

Article by Matthew Casperson (4,883 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 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

Related Files

Images

Screenshot
Showing page 5 of 5
prev1
...
34
5

11 Comments

Showing page 1 of 2 (11 Comments)
Nov 8, 2009 9:24 PM
RE: Flash Game Development with Flex and Actionscript - User Input and an Animated Background
No. Once an object is removed from the system it is returned to the pool. Which object is pulled from the pool for a new object does not matter.

Remember that the pools are specific to a class, so if you are getting an unused Enemy, you are getting that object from a pool that holds only Enemy objects.
Nov 8, 2009 4:12 AM
Konstantin
Still don'd understand
Does it matters what unsused item we get in result?
Nov 7, 2009 6:10 PM
RE: Flash Game Development with Flex and Actionscript - User Input and an Animated Background
See http://www.brighthub.com/hubfolio/matthew-casperson/blog/archive/2009/09/13/keycode-in-flash-flex.aspx for info on keyboard input
Nov 7, 2009 1:47 PM
Aryadi
Keboard input?
How do you do keyboard input btw?
Oct 26, 2009 5:52 AM
RE: Flash Game Development with Flex and Actionscript - User Input and an Animated Background
It will return an object where the inuse property is not true (i.e. the object has been shutdown). The idea is to keep the objects created during the level available for reuse instead of creating new objects and then using the garbage collector to clean them up.
Oct 26, 2009 5:44 AM
Konstantin
Can't understand
Very helpful tutorial. Great job. But I can't understand this part:

public function get ItemFromPool():GameObject
{
for each (var item:GameObject in pool)
{
if (!item.inuse)
return item;
}
var newItem:GameObject = newObject();
pool.addItem(newItem);
return newItem;
}

What item does we get in result? Any unused item or something particular?
Sep 16, 2009 1:01 PM
John
MathUtils.as code
Below is the MathUtils class I created when testing this example out. I salute you Matthew Casperson on a most excellent ActionScript 3 game tutorial!

Simply create a new final class called MathUtils with the static function randomInteger.

Here's the code guys:

package
{
public final class MathUtils
{
public static function randomInteger(low:Number=NaN, high:Number=NaN):Number
{
var low:Number = low;
var high:Number = high;

if(isNaN(low))
{
throw new Error("low must be defined");
}
if(isNaN(high))
{
throw new Error("high must be defined");
}

return Math.round(Math.random() * (high - low)) + low;
}
}
}
Jul 21, 2009 7:47 PM
Source Code
The source code downloads often includes classes and functions that aren't specifically covered by the article itself (like small helper functions in the MathUtils class).
Jul 21, 2009 9:59 AM
Matthias Mueller
RE: Error in part 4
hi,

just create your own MathUtils class and insert a methods like:

public function randomInteger(begin : int, end : int) : int {
return Math.round(Math.floor(start +(Math.random() * (end - start)));
}

Sorry but i cannot test it yet, maybe you got some Type errors and you have to cast between the diffret types.

@ Matthew:
Very nice work, i'll have a look on it at home :D
Jun 26, 2009 12:40 PM
Alan
Error with tutorial 4
Hi, Ive been using your tutorials so far and they have been quite helpful in learning how to use Flex but when I try and run tutorial 4 it gives me the following error Access of undefined property MathUtils. I just wanted to know where this is defined, is it similar to the Math function? Thanks, Alan
Showing page 1 of 2 (11 Comments)
 
Subscribe to Web Development
RSS
Get free weekly updates, directly to your inbox.
Browse Web Development