Game Development Reference
In-Depth Information
The
offset
value is used to ensure that our object rotation values match the actual appearance of
the object. You will notice (a little later in this chapter) that the player's ship is drawn facing up. We
want this to be the 0 rotation value. Flash actually uses the facing-right value as the 0 rotation. By
passing 90 in as the offset, we can mitigate this behavior and create the correct rotation values for
our object. This way, our values will match the look-up table of precalculated vector values created
for optimization of object movement. If the graphic that needs to be rotated was drawn facing right
rather than up, this offset would not be needed. We will draw our graphics for the game facing up,
because we find it much easier to draw via code in this manner. This offset also allows you the
option of using images that are pointed at any angle as the source for the rotation array.
This function will use a
Matrix
to first translate the
BitmapData
(move to a position at
-1/2 width
for
x
and
-1/2 height
for
y
), rotate the original by the new increment, and then translate it back to
its original position. It uses the original
sourceBitmapData
for each rotation to keep skewing and
bitmap degradation to a minimum. The code for the
Matrix
looks like this:
var angleInRadians:Number = Math.PI * 2 * (rotation / 360);
var rotationMatrix:Matrix = new Matrix();
rotationMatrix.translate(-sourceBitmapData.width*.5,-sourceBitmapData.height*.5);
rotationMatrix.rotate(angleInRadians);
rotationMatrix.translate(sourceBitmapData.width*.5,sourceBitmapData.height*.5);
Our second public function is
public function createFadeOutBlitArrayFromBD(sourceBitmapData:BitmapData,
steps:int ):Array{
Like the
createRotationBlitArrayFromBD
function, this one creates an array of faded out
(alpha channel)
BitmapData
assets from the passed in
sourceBitmapData
. The
steps
value
represents the number of frames of animation for the fade out. For example, if the value
10
is
passed in, the function will degrade the alpha of the passed in
BitmapData
(
sourceBitmapData
)
by .1 on each iteration.
This function uses a
ColorMatrixFilter
instance to apply the alpha fade to the
sourceBitmapData
. A full description of
Matrix
operations is beyond the scope of this topic, but
the following
Matrix
will fade out the
sourceBitmapData
when applied to it by the
alpha
value:
var alpha:Number=1 - (ctr*stepAmount)
var alphaMatrix:ColorMatrixFilter = new ColorMatrixFilter(
[1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, alpha, 0]);
Here is the complete source code for this class:
package com.efg.framework
{
import flash.display.*;
import flash.geom.*;
import flash.filters.ColorMatrixFilter;
public class BlitArrayAsset {
public var tileList:Array;
private var point0:Point = new Point(0, 0);