Using Concrete Decorator Classes - Charting souls
(Page 4 of 4 )
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. The getSoul() generates a string, and the good() and evil() 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 the good() and evil()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
{
//Instantiate the two MovieClip objects in
//the Library
var devil:Devil=new Devil();
var angel:Angel=new Angel();
public function Soul()
{
var life:Component=new Jane();
//***Add Good***
life=new Courage(life);
life=new Compassion(life);
life=new Hope(life);
//life=new Integrity(life);
life=new Openness(life);
life=new Diligence(life);
life=new Justice(life);
//***Add Evil***
life=new Malice(life);
life=new Prejudice(life);
//life=new Dogmatisms(life);
life=new Arrogance(life);
//life=new Indifference(life);
//life=new Rage(life);
//life=new Obsfuscation(life);
setAngelDevil(life.good(),life.evil(),life.getSoul());
}
private function setAngelDevil(right:Number,wrong:Number,
eternalsoul:String)
{
this.addChild(devil);
this.addChild(angel);
var booWrong:Number=Number(wrong>0);
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;
}
}
}
}
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. ThesetAngelDevil()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.

Figure 4-7. Decorator generated values used for placement and labels
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.”
Open a new Flash document file, type in Soul in the Document class window, and save the file as SoulChart.fla.
- Add a layer to the existing layer, and name the top layer, Labels and the bottom layer “Lines.” (Say that last sentence fast three times!)
Select Insert -> New Symbol from the menu bar. Type Angel in the Name window, 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.)
- In the Symbol Edit window, draw or import an image of an angel.
- 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.
- Exit the Symbol Edit Mode by clicking the Scene 1 icon. You should now see a movie clip icon in the Library named “Angel.”
- 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.
- Click the Lines layer and add 11 horizontal lines and one vertical line as shown in Figure4-7. Lock the layer.
- 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 adjustments 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.
Please check back next week for the conclusion to this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter four of ActionScript 3.0 Design Patterns Object Oriented Programming Techniques, written by William B. Sanders and Chandima Cumaranatunge (O'Reilly, 2007 ISBN: 0596528469). Check it out today at your favorite bookstore. Buy this book now.
|
|