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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Working with arrays<br />

Arrays example: PlayList<br />

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

The PlayList example demonstrates techniques for working with arrays, in the context of a music playlist application<br />

that manages a list of songs. These techniques are:<br />

Creating an indexed array<br />

Adding items to an indexed array<br />

Sorting an array of objects by differ<strong>en</strong>t properties, using differ<strong>en</strong>t sorting options<br />

Converting an array to a character-delimited string<br />

To get the application files for this sample, see www.adobe.com/go/learn_programmingAS3samples_flash. The<br />

PlayList application files can be found in the Samples/PlayList folder. The application consists of the following files:<br />

File Description<br />

PlayList.mxml<br />

or<br />

PlayList.fla<br />

PlayList class overview<br />

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

The PlayList class manages a set of Song objects. It has public methods with functionality for adding a song to the<br />

playlist (the addSong() method) and sorting the songs in the list (the sortList() method). In addition, the class<br />

includes a read-only accessor property, songList, which provides access to the actual set of songs in the playlist.<br />

Internally, the PlayList class keeps track of its songs using a private Array variable:<br />

public class PlayList<br />

{<br />

private var _songs:Array;<br />

private var _curr<strong>en</strong>tSort:SortProperty = null;<br />

private var _needToSort:Boolean = false;<br />

...<br />

}<br />

In addition to the _songs Array variable, which is used by the PlayList class to keep track of its list of songs, two other<br />

private variables keep track of whether the list needs to be sorted (_needToSort) and which property the song list is<br />

sorted by at a giv<strong>en</strong> time (_curr<strong>en</strong>tSort).<br />

As with all objects, declaring an Array instance is only half the job of creating an Array. Before accessing an Array<br />

instance’s properties or methods, it must be instantiated, which is done in the PlayList class’s constructor.<br />

Last updated 6/6/2012<br />

The main application file in Flash (FLA) or Flex (MXML).<br />

com/example/programmingas3/playlist/PlayList.as A class repres<strong>en</strong>ting a list of songs. It uses an Array to store the list,<br />

and manages the sorting of the list’s items..<br />

com/example/programmingas3/playlist/Song.as A value object repres<strong>en</strong>ting information about a single song. The<br />

items that are managed by the PlayList class are Song instances.<br />

com/example/programmingas3/playlist/SortProperty.as A pseudo-<strong>en</strong>umeration whose available values repres<strong>en</strong>t the<br />

properties of the Song class by which a list of Song objects can be<br />

sorted.<br />

48

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

Saved successfully!

Ooh no, something went wrong!