package
{
import flash.display.*;
public final class ResourceManager
{
[Embed(source="../media/brownplane.png")]
public static var BrownPlane:Class;
public static var BrownPlaneGraphics:GraphicsResource = new GraphicsResource(new BrownPlane());
}
}
In order to use the embedded graphics we need to separate the alpha (or transparent) layer from the original picture. If you look back to the copyToBackBuffer function in the GameObject class you will see that the copyPixels function uses both the bitmap and alphaBitmap properties of a GraphicsResource object. Extracting and exposing these elements from an embedded picture is the sole purpose of the GraphicsResource class. Lets take a look at the GraphicsResource class now.
package
{
import flash.display.*;
public class GraphicsResource
{
public var bitmap:BitmapData = null;
public var bitmapAlpha:BitmapData = null;
public function GraphicsResource(image:DisplayObject)
{
bitmap = createBitmapData(image);
bitmapAlpha = createAlphaBitmapData(image);
}
protected function createBitmapData(image:DisplayObject):BitmapData
{
var bitmap:BitmapData = new BitmapData(image.width, image.height);
bitmap.draw(image);
return bitmap;
}
protected function createAlphaBitmapData(image:DisplayObject):BitmapData
{
var bitmap:BitmapData = new BitmapData(image.width, image.height);
bitmap.draw(image, null, null, flash.display.BlendMode.ALPHA);
return bitmap;
}
}
}
GraphicsResource has the two properties mentioned above: bitmap and bitmapAlpha. These are extracted from an embedded image passed into the constructor by the createBitmapData and createAlphaBitmapData functions. Once extracted they are in a form that is convenient to use with the copyPixels function used in the GameObject’s copyToBackBuffer function.
So what have we achieved here? We have built on the groundwork laid in parts 1 and 2 of the series to embed some resources (ResourceManager and GraohicsResource), created the base class for more specific game objects (GameObject), and then finally combine it all to add a moving object to the screen (Bounce).
In part 4 of the series we will add an object that the player can control, as well as create a scrolling background for the player to fly over.
Go back to Flash Game Development with Flex and ActionScript