Flash_and_Flex_03_2009

Flash DecalSheet System

Step 9 – Returning Decals from the DecalSheet

property along with any passed in values for pixelMapping and smoothing. Next we save a reference of the DecalSheet src in the deaclSheetSrc property. We then save the passed in name value in the inherited name property from the BitmapData . We use this to distinguish the difference between the passed in parameter and the name parameter of the Class's instance. Finally we call the refresh method. The Decal's refresh method performs a simple task. It requests new BitmapData from it's parent DecalSheet and sets it. This creates the display of the Decal. Braking out the logic to request BitmapData from the parent DecalSheet will pay an important role later on when we begin to change the BitampData of the DecalSheet .

any registered Decals. You will notice this short hand conditional. Basically the first item is what we are testing. In this case we want to know if the supplied name has been registered with the decalRectangles Dictonary. The ? denotes what happens if it exists. Here we create a new Decal, give the same name that was passed into the getDecal function and supply a reference of the DecalSheet instance (this) to the Decal. The: denotes what happens if the supplied name was not found on the deacalRectangles Dictonary. We simply return null. Lets test this to make sure everything works. Step 10 – Testing the DecalShet and Decal We are now ready to test our Decal. To do this we will move back over to the Doc Class and

Before we can test that Decals works, we we will want to add the ability to request Decals by name from the DecalSheet and have it return a Decal instance. We can do this by adding the following function in the DecalSheet after the registerDecal method around line 21: public function getDecal(name:String):Decal { return decalRectangles[name] ? new Decal( name, this ) : null; } Now we can request Decals from the DecalSheet by simply passing in the name of

Listing 5. Step 7.1: Testing the sample method

public function decalSheetDemo():void { var sampleBitmap:Bitmap = new Bitmap( decalSheet.sample("up") ); sampleBitmap.x = 230;

addChild(sampleBitmap);

}

Listing 6. Step 8: Creating the Decal Class

package com.flashartofwar {

import flash.display.Bitmap; public class Decal extends Bitmap {

protected var decalSheetSrc:DecalSheet; public function Decal(name:String, src:DecalSheet, pixelSnapping:String="auto", smoothing:Boolean= false ) { super ( null , pixelSnapping, smoothing);

// Save decal sheet source. decalSheetSrc = src;

// Save name of decal so we can sample it from the DecalSheet. this .name = name;

// Get bitmap data from the DecalSheet src. refresh();

} public function refresh():void {

bitmapData = decalSheetSrc.sample(name);

}

}

}

03/2009 (5)

33

Made with FlippingBook HTML5