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.

Adding Properties and Methods<br />

Up to now, we’ve used a single string variable with a single getter method for the<br />

basic abstract <strong>com</strong>ponent class. However, like any other class, this basic structure<br />

can ac<strong>com</strong>modate more than a single variable or function. Example 4-17 shows three<br />

variables and getter functions. Save the script as Component.as.<br />

Example 4-17. Component.as<br />

package<br />

{<br />

//Abstract class<br />

public class Component<br />

{<br />

protected var soul:String="All that is inside a spirit";<br />

protected var goodness:Number;<br />

protected var vice:Number;<br />

}<br />

}<br />

public function getSoul( ):String<br />

{<br />

return soul;<br />

}<br />

public function good( ):Number<br />

{<br />

return goodness;<br />

}<br />

public function evil( ):Number<br />

{<br />

return vice;<br />

}<br />

Like the previous examples, we begin with a string property, soul. (It’s assigned a<br />

string literal, but that’s really not necessary because it’s an abstract class and will<br />

never be seen or used—just a clarification.) Next, two numeric properties are<br />

defined, goodness and vice. These two properties will collect all the accumulated<br />

good and evil in a soul.<br />

Next, three getter functions are supplied to get the values of the string and two<br />

numeric variables. Now the abstract <strong>com</strong>ponent class is all set to go.<br />

Multiple Concrete Components<br />

We could look at one soul at a time, but what fun is that? More important, in a lot of<br />

applications, having a single <strong>com</strong>ponent to decorate isn’t too useful either. So<br />

instead of having a single soul to decorate, we’ll add two. For the time being, we’ll<br />

forego any debates about original sin and start our souls with a clean slate. Both<br />

goodness and vice will be set to zero. Just give them different soul values so they can<br />

be differentiated.<br />

Decorating with Deadly Sins and Heavenly Virtues | 149

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

Saved successfully!

Ooh no, something went wrong!