Game Development Reference
In-Depth Information
Here is the entire set of Library class code:
package com.efg.games.notanks
{
import flash.display.Bitmap;
import flash.display.BitmapData;
/**
* ...
* @author Jeff Fulton
*/
public class Library {
[Embed(source='../../../../../assets/tanks_sheet.png')]
public static const TankSheetPng:Class;
}
}
This class is really very simple. It embeds the tanks_sheet.png file and gives it an export class
name of TankSheetPng . Notice the location of the PNG file is set relative to the package folder. This
is very important to remember. We will use this static constant ( TankSheetPng ) in our game code in
much the same way as the IDE library uses the class name when a library item is set for export.
Adding the library assets for the Flash IDE only
If you are using the Flash IDE, you will need to import the tanks_sheet.png file into the library and
set the export name to be TankSheetPng . Notice that this is the exact same name we used for the
Flex embed class name. Keeping these consistent will make porting from one technology to the
other a trivial matter.
Using the library assets in Flex and the Flash IDE
Now that we have the asset in the library of Flash Develop/Flex or the Flash IDE, we are ready to
use the asset. As a quick example, this is the code we will use in the Flex version to pass the
TankSheetPng class instance to the TileSheet class that we will create in the next section.
//***** Flex *****
private var tileSheet:TileSheet= new TileSheet(new Library.TankSheetPng().bitmapData,
tileWidth, tileHeight);
//***** End Flex *****
Notice that the code creates a new instance of the TankSheetPng class and passes the
bitmapData attribute of it directly into the TileSheet constructor. We do this because the
TileSheet class constructor requires us to pass in a BitmapData instance, not a Bitmap instance.
Since Flex only allows us to embed bitmap assets as a Bitmap class (actually BitmapAsset ), to
get to the bitmapData , we have to reference it with the dot ( . ) syntax.
Conversely, in the Flash IDE, bitmap assets in the library are given the base class of BitmapData .
So, our call to the constructor or the TileSheet class is a little simpler.
//***** Flash IDE *****
//private var tileSheet:TileSheet = new TileSheet(new TankSheetPng(0,0), tileWidth,
tileHeight);
//***** End Flash IDE *****
The big difference is the constructor for the TankSheetPng requires that two parameters be passed
or a compile-time error will occur. These values can be the actual size of the tank_sheet.png file,
Search WWH ::




Custom Search