Adding Weapons to Your Game: Flex & Actionscript Tutorial

Written by:  • Edited by: Linda Richter
Updated Nov 6, 2009

In part 4 of the series we added user input, and created a scrolling background to simulate flying over an ocean. In part 5 we will add some enemy fighters and player weapons.

At this point in the series we have implemented enough underlying code to make adding new elements to the game quite easy. With GameObjectManager and GameObject classes handling the work of drawing and updating the game elements and the Level in place to actually create the new elements, there is only a minimal amount of code required to implement new game elements. We will take advantage of this to add some enemy fighters to the game, and give the player some weapons to fight them with.

First, let’s take a look at the changes that have been made to the Level class.

Some More Action

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 TimeBetweenEnemies:Number = 3;

protected static const TimeBetweenClouds:Number = 2.5;

protected var timeToNextLevelElement:Number = 0;

protected var levelElementGraphics:ArrayCollection = new ArrayCollection();

protected var timeToNextEnemy:Number = 0;

protected var enemyElementGraphics:ArrayCollection = new ArrayCollection();

protected var timeToNextCloud:Number = 0;

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

enemyElementGraphics.addItem( ResourceManager.SmallBluePlaneGraphics);

enemyElementGraphics.addItem( ResourceManager.SmallGreenPlaneGraphics);

enemyElementGraphics.addItem( ResourceManager.SmallWhitePlaneGraphics);

}

public function startup():void

{

timeToNextLevelElement = 0;

new Player().startupPlayer();

}

public function shutdown():void

{

}

public function enterFrame(dt:Number):void

{

// 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 an enemy

timeToNextEnemy -= dt;

if (timeToNextEnemy <= 0)

{

timeToNextEnemy = TimeBetweenEnemies;

var enemygraphics:GraphicsResource = enemyElementGraphics.getItemAt( MathUtils.randomInteger(0, enemyElementGraphics.length)) as GraphicsResource;

var enemy:Enemy = Enemy.pool.ItemFromPool as Enemy;

enemy.startupBasicEnemy(

enemygraphics,

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

55);

}

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

}

}

}

}

The timeToNextEnemy / TimeBetweenEnemies and timeToNextCloud / TimeBetweenClouds pair of properties serve the same function as the timeToNextLevelElement / TimeBetweenLevelElements pair of properties (which was detailed in part 4) except they are used to add enemies and clouds respectively. The enemyElementGraphics property is used for the same purpose as the levelElementGraphics (which was also detailed in part 4) except here is it used to provide GraphicsResource’s when creating new enemies.

We have also added two new blocks of code in the enterFrame function to create the enemies and the clouds. This code is almost exactly the same as the block of code that is used to create the BackgroundLevelElement’s.

The clouds are implemented as a BackgroundLevelElement, except with a slightly faster scroll rate and a higher zOrder. This gives them the appearance of being at a higher altitude.

A new class, Enemy, has been created to represent the enemy fighters. Let’s look at that code now.

Showing page 1 of 3

Comments

Showing all 6 comments
 
Matthew Casperson May 1, 2010 10:12 PM
RE: Adding Weapons to Your Game: Flex & Actionscript Tutorial
dt = delta time (delta meaning change)
thx May 1, 2010 2:56 PM
RE: Adding Weapons to Your Game: Flex & Actionscript Tutorial
dt = due time ?

thx for the tutorial
Troy Scheffel Jan 31, 2010 12:17 AM
RE: timeToNextEnemy & Math.Random()
@Aryadi: I'll attempt to answer your questions in case someone surfs on in down the road (like I did).

Q: "...while the timeToNextEnemy is already 0 I don't see the need to reducing the timeToNextEnemy..."?
A: True, it will be zero the first time through (since it was initialized to zero) but it won't be zero the second time through since it will be reset (to TimeBetweenEnemies) within the IF block.

Q: "...does Math.randon() return values between 0 and 1?"
A: Yes. I'm a noob AS3 programmer so I googled "Flex 3 Math.Random()" and found out it works like Random() for most other languages: it returns a pseudo-random number n, where 0.0 <= n < 1.0.

@Paul:

Q: "what is dt?"
A: As someone already mentioned, dt stands for Delta Time, meaning the time elapsed between present time and some past event (such as elapsed time between frame updates). On fast computers with high FPS I would expect dt to be smaller than on slower computers with lower FPS. Old computer games that didn't account for dt (or similar mechanism) are often unplayable on new systems (unless you can artificially slow them down, e.g. waste cpu cycles).

Happy Coding
Aryadi Oct 21, 2009 12:20 PM
just wanna say....
this tutorial really great! thanks for writing it

and also, I have a little question:

I've been trying to understand the code and it seems like the enterFrame logic resolves around the delta time. Like, reducing the timeToNextEnemy with dt and when the timeToNextEnemy value is or below 0 then do something, while the timeToNextEnemy is already 0 I don't see the need to reducing the timeToNextEnemy (it's already 0, the if conditional is true already)

and I wanna know, does Math.randon() return values between 0 and 1? that would be the logical reason how the enemy player came out randomly on the stage

I think I have some more questions, but I guess that's it for now. I'll ask again when I remember what to ask about :)
easyDaMan Aug 18, 2009 7:29 AM
dt is...
the delta (d) of the time (t) that has passed since the last frame... i guess! in other words: the difference between now and then!

var now:Number - var then:Number = dt:Number! and dt becomes the new now!

i love u!

peace
paul Aug 3, 2009 12:38 AM
brillianrt
excellent tutorials.

I stuck on what (dt) is though?
 
blog comments powered by Disqus
Email to a friend