Flash_and_Flex_03_2009

ActionScript Development

independent. That is why we are going to add a detach method to the Decal Class after the onChange method:

the reference to the DecalSheet src. This completely disconnects any relation to the parent DecalSheet . Step 18 – Creating a DecalSheet Interface One of the fundamental concepts of building OO ( Object Oriented ) code and design patterns is to Program to an Interface; not an implementation . Unfortunately to fully explain this concept is out of the scope of this tutorial but that still doesn't mean we can't try to instill some best practices. If you have never used Interfaces before they are very simple. All an interface does is define a set of Public functions a class must contain. So in our DecalSheet system we are going to create an IDecalSheet interface and type our Decal to it. This will keep our decals loosely coupled to the DecalSheet and allow the most amount of flexibility when extending our system. To get started we will need to create a new Interface in the same package as our DecalSheet and Decal Classes. Here is the structure of the Interface, see (Listing 11). So in our interface we are defining the most commonly used public functions that make up our DecalSheet . Also, take note that even our Interface can extend other interfaces. We are going to extend IBitmapDrawable and IEventDispatcher . This will allow our DecalSheet to do the same tasks as the Bitmap class and we will be able to dispatch and listen to events from it. Now we need to tell the DecalSheet to implement this Interface. If you go into the DecalSheet Class and replace the class definition around line 10 with the following: public class DecalSheet extends Bitmap implements IDecalSheet If your Interface is in the same package as your DecalSheet you do not have to worry about importing the IDecalSheet interface. Next we have to implement the interface in our Decal Class. Whereever we use the type DecalSheet, we will want to replace it with IDecalSheet :

removeListeners(decalSheetSrc); decalSheetSrc = null;

}

Once the detach method is called, we remove the Event listeners as well as null out

public function detach():void {

Listing 10. Step 16: Get Registered Decal Names

public function registerDecal(name:String, rectangle:Rectangle):void { decalRectangles[name] = rectangle; decalNames.push(name); } public function deleteDecal(name:String):Boolean { var index:Number = decalNames.indexOf(name); if (index != -1) decalNames.splice(index,1); return delete decalRectangles[name]; }

Listing 11. Step 18: Creating a DecalSheet Interface

package com.flashartofwar {

import flash.display.BitmapData; import flash.display.IBitmapDrawable; import flash.events.IEventDispatcher; import flash.geom.Rectangle; public interface IDecalSheet extends IBitmapDrawable, IEventDispatcher { function registerDecal(name:String, rectangle:Rectangle):void; function deleteDecal(name:String):Boolean;

function getDecal(name:String):Decal; function sample(name:String):BitmapData;

}

} Listing 12. Step 19: Clear Decals from the DecalSheet

public function clear():void {

dispatchEvent( new Event(Event.DEACTIVATE, true , true )); decalRectangles = new Dictionary( true ); decalNames = new Array();

} Listing 13. Step 19.1: Clear Decals from the DecalSheet

protected function addListeners(target:IDecalSheet):void { target.addEventListener(Event.CHANGE, onChange, false , 0, true ); target.addEventListener(Event.DEACTIVATE, onDeactivate); } protected function removeListeners(target:IDecalSheet):void { target.removeEventListener(Event.CHANGE, onChange); target.removeEventListener(Event.DEACTIVATE, onDeactivate); }

Around line 9:

protected var decalSheetSrc:IDecalSheet;

Around line 11:

public function Decal(name:String, src: IDecalSheet,

pixelSnapping: String="auto", smoothing: Boolean=false)

03/2009 (5)

36

Made with FlippingBook HTML5