384 | Chapter 10: State Pattern
Note that instead of naming the method for stopping the video play, it’s been
changed tostopAll( )in line 10. The reason for this is that in the different states,
stopping means something different. It can mean stop recording and appending in
addition to stop playing the video. So the change focuses on the fact that it’s not just
to do one thing. It’s another instance where polymorphism is coming in handy.
Next, theStopStateclass has oneNetStreammethod that’s part of an older Action-
Script, Client-Side ActionScript (CSAS) from Flash Media Server 2. In ActionScript 3.
0, theNetStream.play( )method expects only a single argument—a string for the
FLV file’s URL. However, in CSAS, you can add a second parameter to specify what
type of stream to play. In order for AS 3.0 to work with classes from prior versions of
ActionScript that serialize objects, the application will have to import the
ObjectEncodingclass. However, if that’s done, AS 3.0 can fully integrate these other
objects and their parameters. Line 20 shows this second argument added to the
NetStream.play( ) method.
Figure 10-7. Statechart with five states
Example 10-20. 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 startRecord(ns:NetStream, flv:String):void;
9 function startAppend(ns:NetStream, flv:String):void;
10 function stopAll(ns:NetStream):void;
11 function doPause(ns:NetStream):void;
12 }
13 }
Stop
Play Pause
Record
Append