Game Development Reference
In-Depth Information
Creating the movement vector look-up table
Follow these steps to create the movement lookup table:
1.
We will add a global
Array
or
Vector
variable to our
BlasterMines.as
class that will hold
Point
object instances with the
dx
and
dy
values needed to move our game objects in
direction of their rotation. The x of the
Point
will be our
dx
value and the y of the point will
be our
dy
value. This uses slightly less memory than a generic
Object
instance, because
it is
Finalized
and not dynamic, which saves the Flash Player from having to allocate
memory just in case the generic
Object
instance needs to add properties on the fly. All of
the following code will be in the
com.efg.games.blastermines.BlasterMines.as
file. First
we have the lines from the variable definition section:
//math look up tables
//private var rotationVectorList:Array = [];
private var rotationVectorList:Vector.<Point>=new Vector.<Point>(360,false);
2.
The
init
portion of the
BlasterMines.as
class will call a function that will create the
look-up table:
createLookupTables();
3.
The
createLookUpTables
function will loop through 0 to 359 and calculate the
dx
and
dy
vector values needed to move an object to that rotation:
private function createLookupTables():void {
for (var ctr:int = 0; ctr < 359; ctr++) {
var point:Point = new Point();
point.x = Math.cos((Math.PI * ctr) / 180);
point.y = Math.sin((Math.PI * ctr) / 180);
rotationVectorList[ctr] = point;
}
}
When one of the game objects needs to move into a particular direction based on a rotated angle,
it will use the
rotationVectorList
instead of calculating the
dx
and
dy
values on the fly. Let's take
a look at an example of this in the next section.
Accessing the vectorRotationList look-up table
All of the display objects will be moved at angles in the game. Let's look at how the
Projectile
object instances will be created and access this table.
The player's projectile missiles are automatically fired each frame tick with a three-frame delay
between each shot. The projectiles will be pooled much like the particles. In the
update
function of
BlasterMines.as
, the projectiles will be created. Here is the code that does just that; this code will
be in the
com.efg.games.blastermines.BlasterMines.as
file:
//*** auto fire
projectileManager.projectilePoolCount = projectileManager.projectilePool.length - 1;
if (projectileManager.lastProjectileShot > 3 && projectileManager.projectilePoolCount
> 0 && playerStarted && mineManager.mineCount > 0) {