O'Reilly logo

ActionScript 3.0 Design Patterns by Chandima Cumaranatunge, William Sanders

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Video Player Concrete State Application

All that you've seen so far has been the output as trace() statements to help understand how a State design pattern and State machine works. To add something useful, we need to include a reference to both a NetStream object and a string for referencing an FLV file. However, we need a string reference only for playing the video, because we can stop it simply by closing the NetStream instance. The following four scripts set up the state machine to actually play and stop a video. All of the trace statements have been left in place.

With the implementation of an application that actually plays a video, we'll need to import the necessary parts. Because the NetStream class is used in the interface and the two states, each of those files will need to import the class. However, while the VideoWorks class uses both the Play and Stop classes, it does not have to import the NetStream() class. This is because it's already imported in the Play and Stop classes.

The following five listings, Example 10-7 through Example 10-11, should be entered into an ActionScript file and saved with the captions as the filenames. Save all the files in the same folder.

Example 10-7. State.as

1           package
2           {
3               //State Interface #1
4               import flash.net.NetStream;
5               interface State
6               {
7                   function startPlay(ns:NetStream,flv:String):void;
8                   function stopPlay(ns:NetStream):void;
9               }
10       }

In the following StopState class shown in Example 10-8, you will note that the startPlay() method ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required