• Aucun résultat trouvé

Implementing the Good and Evil Decorator

Dans le document ActionScript 3.0 Design Patterns (Page 181-185)

Instead of a single implementation, you can try out two different implementations.

The first one dresses up the two different concrete components, and sends the results to the output window. The second takes the results, uses them to place movie clips on a “soul graph,” and adds a label to an angel or devil movie clip—depending on whether good or evil is predominant.

Dual implementation

The first implementation decorates both the Dick and Jane concrete components.

This one is set up to use all 14 deadly sins and heavenly virtues, but you can use any combination you want. As each concrete component (lifeandlight) is wrapped, the good and evil properties are incremented or decremented, depending on which decorator wraps the concrete component. The reference to thecomponentsobject in each of the decorators is a reference to the concrete component being wrapped. With each new decorator, then, the value goes up and down depending on the wrapping decorator. Save Example 4-36 as MainDual.as in an ActionScript file. Open a new Flash document file, and save it as a Dual.fla. Type in MainDual in the Document Example 4-35. Indifference.as

package {

public class Indifference extends Decorator {

private var components:Component;

public function Indifference(components:Component) {

this.components=components;

}

override public function getSoul( ):String {

return components.getSoul( ) + "|Indifference";

}

override public function good( ):Number {

return -9 + components.good( );

}

override public function evil( ):Number {

return 10 + components.evil( );

} } }

class window in the Dual.flaProperties panel, and resave it as MainDual.as. Now test the movie.

The output for all of the good and evil together is:

||--Judging--||

Because thetrace( )statement subtracts theevil( )method from thegood( )method total value, any positive results indicate an abundance of good characteristics, and a negative result shows a plethora of negative traits.

You can “multi-wrap” using the same decorator more than once on the same con-crete component. (Someone who thinks he can get the month-behind project done Example 4-36. MainDual.as

package {

import flash.display.Sprite;

public class MainDual extends Sprite {

on time by not sleeping for a week is doubly hopeful, has a triple dose of diligence, and perhaps a double dose of arrogance.)

Charting souls

Like any of the other design patterns, what you do with the output is up to you.

However, because ActionScript 3.0 is part of Flash, this next implementation shows how to place the output into different formats with graphic elements. ThegetSoul( ) generates a string, and thegood( )andevil( )methods generate numbers. The string will be placed in a text field embedded in a movie clip, and the vertical position of the movie clip will be determined by the values generated by thegood( )andevil( ) methods. To get started, save the script in Example 4-37 in an ActionScript file namedSoul.as.

Example 4-37. Soul.as package

{

import flash.display.Sprite;

import flash.display.MovieClip;

public class Soul extends Sprite {

private function setAngelDevil(right:Number,wrong:Number, eternalsoul:String)

{

this.addChild(devil);

this.addChild(angel);

var booWrong:Number=Number(wrong>0);

In looking at the script, all it does is pass the three different values generated by the getter methods,good( ),evil( )andgetSoul( ), to thesetAngelDevil( )function. The setAngelDevil( )function uses two movie clips from the library and positions them on the stage. Depending on the outcome, the concrete component’s name appears in the angel or devil icon. Figure 4-7 shows what the output will look like. Use it as a guide for setting up your stage.

var booRight:Number=Number(right>0);

devil.x=330;

devil.y=270-((wrong*booWrong)*(270/72));

angel.x=96;

angel.y=270-((right*booRight)*(270/60));

if (booWrong) {

devil.soul_txt.text=eternalsoul;

} else {

angel.soul_txt.text=eternalsoul;

} } } }

Figure 4-7. Decorator generated values used for placement and labels Example 4-37. Soul.as (continued)

The following steps guide you through the process of preparing the two MovieClip classes in the Flash IDE and setting the stage as a “soul chart.”

1. Open a new Flash document file, type in Soulin the Document class window, and save the file asSoulChart.fla.

2. Add a layer to the existing layer, and name the top layer,Labelsand the bottom layer “Lines.” (Say that last sentence fast three times!)

3. Select Insert New Symbol from the menu bar. TypeAngelin the Name win-dow, and click the Export for ActionScript checkbox. Click OK. You are now in the Symbol Edit Mode. (Be sure to capitalize the “A” in “Angel” because this is a class name.)

4. In the Symbol Edit window, draw or import an image of an angel.

5. Click the Text icon in the Tools panel, and select Dynamic Text for the type of text. In the Properties panel, provide the instance namesoul_txtfor the dynamic text object.

6. Exit the Symbol Edit Mode by clicking the Scene 1 icon. You should now see a movie clip icon in the Library named “Angel.”

7. Repeat steps 3 to 6, substituting “Devil” for “Angel.” Once you’re finished, you should see both Devil and Angel movie clip icons in the Library panel.

8. Click the Lines layer and add 11 horizontal lines and one vertical line as shown in Figure 4-7. Lock the layer.

9. Click the Labels layer, number the lines from 0 to 100, and place a “Good” and

“Evil” label at the top of the stage as shown in Figure 4-7. Lock the layer and save the file once again.

By adding and removing the comment lines in theSoul.asfile, you can change the vertical positions of the angel and devil images. You may have to make some adjust-ments depending on the size of your angel and devil movie clips and/or if you change the default size of the stage from 550 by 400.

Dynamic Selection of Concrete Components and

Dans le document ActionScript 3.0 Design Patterns (Page 181-185)