13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

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.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with arrays<br />

Combining array elem<strong>en</strong>ts into a character-delimited string<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

In addition to using an array to maintain the song list in the PlayList class, in this example arrays are also used in the<br />

Song class to help manage the list of g<strong>en</strong>res to which a giv<strong>en</strong> song belongs. Consider this snippet from the Song class’s<br />

definition:<br />

private var _g<strong>en</strong>res:String;<br />

public function Song(title:String, artist:String, year:uint, fil<strong>en</strong>ame:String, g<strong>en</strong>res:Array)<br />

{<br />

...<br />

// G<strong>en</strong>res are passed in as an array<br />

// but stored as a semicolon-separated string.<br />

this._g<strong>en</strong>res = g<strong>en</strong>res.join(";");<br />

}<br />

Wh<strong>en</strong> creating a new Song instance, the g<strong>en</strong>res parameter that is used to specify the g<strong>en</strong>re (or g<strong>en</strong>res) the song<br />

belongs to is defined as an Array instance. This makes it conv<strong>en</strong>i<strong>en</strong>t to group multiple g<strong>en</strong>res together into a single<br />

variable that can be passed to the constructor. However, internally the Song class maintains the g<strong>en</strong>res in the private<br />

_g<strong>en</strong>res variable as a semicolon-separated String instance. The Array parameter is converted into a semicolonseparated<br />

string by calling its join() method with the literal string value ";" as the specified delimiter.<br />

By the same tok<strong>en</strong>, the g<strong>en</strong>res accessors allow g<strong>en</strong>res to be set or retrieved as an Array:<br />

public function get g<strong>en</strong>res():Array<br />

{<br />

// G<strong>en</strong>res are stored as a semicolon-separated String,<br />

// so they need to be transformed into an Array to pass them back out.<br />

return this._g<strong>en</strong>res.split(";");<br />

}<br />

public function set g<strong>en</strong>res(value:Array):void<br />

{<br />

// G<strong>en</strong>res are passed in as an array,<br />

// but stored as a semicolon-separated string.<br />

this._g<strong>en</strong>res = value.join(";");<br />

}<br />

The g<strong>en</strong>resset accessor behaves exactly the same as the constructor; it accepts an Array and calls the join() method<br />

to convert it to a semicolon-separated String. The get accessor performs the opposite operation: the _g<strong>en</strong>res<br />

variable’s split() method is called, splitting the String into an array of values using the specified delimiter (the literal<br />

string value ";" as before).<br />

Last updated 6/6/2012<br />

51

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

Saved successfully!

Ooh no, something went wrong!