ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Example: Music Playlists | 219

The Playlistclass extends theComponent class and is almost identical to the


composite class in the minimalist example.


Queuing Songs to Play in Sequence


Much of theSongclass shown in Example 6-10 deals with queuing songs and han-


dling events. Because songs need to be played in sequence and not simultaneously,


several static properties are declared in the class. ThesoundChannel property is


declared asstaticto ensure that only one sound channel is used to play music by all


instances of theSongclass. This ensures that different song objects don’t open multi-


ple sound channels simultaneously to play music. TheSongclass also declares a static


property calledaSongQueue, which is an array that holds the list of songs queued to


play. Finally, a static property calledsongPlayingFlagholds a Boolean value that


indicates whether a song’s currently playing.


private var aChildren:Array;


public function Playlist(sName:String)
{
this.sName = sName;
this.aChildren = new Array( );
}


override public function add(c:Component):void
{
aChildren.push(c);
}


override public function play( ):void
{
trace("Queuing playlist: " + this.sName);
for each (var c:Component in aChildren)
{
c.play( );
}
}
}
}


Example 6-10. Song.as


1 package
2 {
3 import flash.events.*;
4 import flash.media.Sound;
5 import flash.media.SoundChannel;
6 import flash.net.URLRequest;
7
8 public class Song extends Component
9 {

Example 6-9. Playlist.as (continued)

Free download pdf