10.12.2012 Views

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Example 3-10. Alert.as<br />

}<br />

}<br />

{<br />

Alert._instance=new Alert(new PrivateClass( ));<br />

trace("Alert instantiated");<br />

}<br />

return Alert._instance;<br />

}<br />

//Alert Property<br />

public var colorFont:uint;<br />

//Alert Methods<br />

public function getMsg( ):String<br />

{<br />

return _msg;<br />

}<br />

public function setMsg(alert:String):void<br />

{<br />

_msg = alert;<br />

}<br />

class PrivateClass<br />

{<br />

public function PrivateClass( ) {<br />

trace("PrivateClass called");<br />

}<br />

}<br />

Save the file as Alert.as. As you can see, this Singleton design pattern doesn’t have to<br />

be saved as “Singleton,” and the only reason that name was used in the previous<br />

examples was to help you better see its structure, and where it belonged in the application.<br />

In fact, it’s almost identical to the other Singleton.as files used as examples in<br />

this chapter, and even has the same methods as in Example 3-8. However, this example<br />

includes a property used as a variable in the font color of the text field used here.<br />

Conceivably, your application may need different colored messages to differentiate<br />

categories of advisements your application might employ.<br />

In <strong>ActionScript</strong> <strong>3.0</strong>, you can create a TextField instance in one of two ways. First,<br />

you can create it dynamically in the script, as we will do in this example. Alternatively,<br />

you can create a MovieClip object on the stage, embed a TextField in the<br />

movie clip, and then reference the embedded TextField. Unless you have a good reason<br />

for doing so, it’s more practical and uses less memory to just write the script for<br />

the TextField. (Further on in this chapter, a movie clip in the shape of a shopping<br />

cart with an embedded text field shows how to create and dynamically use a<br />

TextField object on the stage.)<br />

Open a new <strong>ActionScript</strong> file and after entering the code in Example 3-11, name the<br />

file AlertText.as and save it in the same folder as Alert.as.<br />

114 | Chapter 3: Singleton Pattern

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!