Flash_and_Flex_03_2009

ActionScript Development

There is a lot going on in this function so lets go line by line through the process.

Now we have our composited BitmapData and we simply return the new BitmapData instance. Step 7 – Testing the sample method Before we go any further we are going to want to do a few simple things to test out new DecalSheet sample method. Lets go back into out Doc Class and add the following function at line 48 after the onImageLoad method, see (Listing 4). Here you can see we register each of the button states we are going to need later when we create our SimpleButton . You will need to import the Rectangle class on line 11:

var bmd:BitmapData = new

BitmapData(rect.width, rect.height, true, 0xffffff);

var rect:Rectangle = decalRectangles[name];

As you can see we use the Rectangle's width and height as the new dimensions, set the transparent parameter to true, and give the new BitmapData a background color of 0xffffff . By setting transparent to true and supplying a background color we will be able to correctly sample transparent png images like the button_skin examples we are loading in. Finally we need to draw the DecalSheets bitmapData into the new BitmapData Class instance, and apply the Matrix.

Here, we use the passed in Decal name to look up the registered Rectangle from the decalRectangles Dictonary. Next we are going to create a Matrix to offset where we sample the BitmapData from.

var m:Matrix = new Matrix();

m.translate(-rect.x,-rect.y);

Here we use the Rectangle's x and y position to create the appropriate sample offset. Now we have to create new BitmapData to store our cutout.

bmd.draw(bitmapData, m);

import flash.geom.Rectangle;

as well as adding the call for registerDecals on line 46, inside of the onImageLoad function after we add the DecalSheet to the stage.

Listing 2. Step 2: Creating the DecalSheet Class

package com.flashartofwar {

import flash.display.BitmapData; import flash.display.Bitmap; public class DecalSheet extends Bitmap { public function DecalSheet(bitmapData:BitmapData= null , pixelSnapping: String="auto", smoothing:Boolean= false ) { super (bitmapData, pixelSnapping, smoothing); }

RegisterDecals();

Now we are going to create one last function at the bottom of our class, around line 54, see (Listing 5). This function will be our main staging area for the rest of the demo. Right now we are creating a new Bitmap from the DecalSheet's up registeredDecal coordinates, offseting its x position and adding it to the stage. We will call this function after the registerDecals call we added to the onImageLoad around line 46: Now if we do a compile, we should see our DecalSheet image on the left, and our sampled Bitmap from the DecalSheet on the right. You can test out all of the Decals by changing up for down or over . Now we have enough to start our Decal class. Step 8 – Creating the Decal Class. Just like the DecalSheet , the Decal will also extend the Bitmap Class. The Decal however will have a very specialized purpose and will rely on the DecalSheet to supply it's BitmapData instead of having it passed into the constructor. Create a new Class called Decal and paste in the following code, see (Listing 6). So what is going on? As you can see we are changing the constructor's arguments from the original BitmapClass's. Our Decal is going to need to know its name (we use this to request BitmapData from the DecalSheet through he sample method) and we need to know the src DecalSheet the Decal was cut out from. Going through the construction process, we pass in null to the super's BitmapData decalSheetDemo();

}

} Listing 3. Step 6: DecalSheet sample method

public function sample(name:String):BitmapData { var rect:Rectangle = decalRectangles[name];

// Applies the correct offset when sampling the data var m:Matrix = new Matrix(); m.translate(-rect.x,-rect.y);

// Creates new BitmapData var bmd:BitmapData = new BitmapData(rect.width, rect.height, true , 0xffffff); bmd.draw(bitmapData, m); return bmd; } Listing 4. Step 7: Testing the sample method

public function registerDecals():void {

decalSheet.registerDecal("up", new Rectangle(0,0,99,31) ); decalSheet.registerDecal("down", new Rectangle(0,32,99,31) ); decalSheet.registerDecal("over", new Rectangle(99,0,99,31) ); }

03/2009 (5)

32

Made with FlippingBook HTML5