ActionScript 3.0 Design Patterns

(Chris Devlin) #1

146 | Chapter 4: Decorator Pattern


Once you’ve got everything ready to go, you’ll need some GIF files. The model (con-


crete component) should be an image of a doll that you will clothe with the Decora-


tor pattern. Each filename needs to be the name of the value assigned to the


whatToWearvariable in the concrete component or decorator listing, minus the tilde


(~) character. For example, the concrete component,Sue,whatToWearvariable is


assigned the string value “~sue” and so the GIF filename for that component would


besue.gif. The decoratorBlueDresshas awhatToWearvalue of “~bluedress,” and so


the image with the blue dress would be namedbluedress.gif. Place all the GIF files,


including the file representing the doll model, in a folder namedclothes.


Finally, open up a new Flash document and set the stage dimensions to 300×650.


In the Properties panel’s Document class window, type inFashionShow. The size and


shape of your stage will depend on the size of the paper dolls you use.


Figure 4-6 shows two different combinations of decorations on the paper doll. The


image on the left decorates with the hat, bow, orange dress, and muff, while the sec-


ond is the blue dress, the bow and an umbrella. Each appears very different just by


changing the combination of decorations.


trace("||--Working--||");
var doll:Model = new Sue( );
doll=new Hat(doll);
doll=new OrangeDress(doll);
//doll=new BlueDress(doll);
//doll=new Umbrella(doll);
doll=new Bow(doll);
doll=new Muff(doll);
var ready2wear:String=doll.getDressed( );
sewingMachine(ready2wear);
for (x=0; x < ensemble.length; x++)
{
clothesOn(ensemble[x]);
}
}
private function sewingMachine(wardrobe:String):Array
{
ensemble=wardrobe.split("~");
ensemble.shift( );
return ensemble;
}
private function clothesOn(outfit:String)
{
var clothier:Loader=new Loader( );
var item:String="clothes/" + outfit + ".gif";
var getItem:URLRequest=new URLRequest(item);
clothier.load(getItem);
this.addChild(clothier);
}
}
}


Example 4-16. FashionShow.as (continued)

Free download pdf