ActionScript 3.0 Design Patterns

(Chris Devlin) #1
When to Use the Singleton Pattern | 115

When using something other thantrace( )statements in your applications, you need


to import the necessary packages and classes for your application. Also, anything


that you plan to place on the stage needs to be at least a Sprite or MovieClip. Because


our application has no Timeline, we can use aSprite. Thus, theAlertTestextends


theSprite class.


We need to mention a couple of concepts here for OOP beginners and those new to


ActionScript 3.0. They includepackage parsimonyand abstraction. As noted in


Chapter 1, abstraction is a pillar of OOP, and you can see it in the Alert class where


parameters and return values are abstract variables. The conceptpackage parsimony


reminds us that when importing packages from the core set, you should import only


what you need, and not the whole package. As you can see in theAlertTestclass,


only the necessary classes were imported. While it can be tempting to use the wild-


card asterisk (*) to bring in the whole package and save some typing time, this car-


ries with it a lot of baggage. For example, instead of typing in the three lines,


import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;

we could have just typed in,


import flash.text.*;

Example 3-11. AlertText.as


package
{
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;
import flash.display.Sprite;
public class AlertTest extends Sprite
{
public function AlertTest( )
{
var reminder:Alert = Alert.getInstance( );
reminder.setMsg("Remember to use addChild( )");
var announce:TextField=new TextField( );
announce.text=reminder.getMsg( );
announce.autoSize=TextFieldAutoSize.LEFT;
var format:TextFormat=new TextFormat( );
reminder.colorFont=0x990099;
format.color=reminder.colorFont;
format.font="Arial Black";
announce.setTextFormat(format);
announce.x=200;
announce.y=150;
this.addChild(announce);
}
}
}

Free download pdf