10.12.2012 Views

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

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.

Figure 10-7. Statechart with five states<br />

Example 10-20. State.as<br />

1 package<br />

2 {<br />

3 //State Interface #1<br />

4 import flash.net.NetStream;<br />

5 interface State<br />

6 {<br />

7 function startPlay(ns:NetStream, flv:String):void;<br />

8 function startRecord(ns:NetStream, flv:String):void;<br />

9 function startAppend(ns:NetStream, flv:String):void;<br />

10 function stopAll(ns:NetStream):void;<br />

11 function doPause(ns:NetStream):void;<br />

12 }<br />

13 }<br />

Note that instead of naming the method for stopping the video play, it’s been<br />

changed to stopAll( ) in line 10. The reason for this is that in the different states,<br />

stopping means something different. It can mean stop recording and appending in<br />

addition to stop playing the video. So the change focuses on the fact that it’s not just<br />

to do one thing. It’s another instance where polymorphism is <strong>com</strong>ing in handy.<br />

Next, the StopState class has one NetStream method that’s part of an older Action-<br />

Script, Client-Side <strong>ActionScript</strong> (CSAS) from Flash Media Server 2. In <strong>ActionScript</strong> 3.<br />

0, the NetStream.play( ) method expects only a single argument—a string for the<br />

FLV file’s URL. However, in CSAS, you can add a second parameter to specify what<br />

type of stream to play. In order for AS <strong>3.0</strong> to work with classes from prior versions of<br />

<strong>ActionScript</strong> that serialize objects, the application will have to import the<br />

ObjectEncoding class. However, if that’s done, AS <strong>3.0</strong> can fully integrate these other<br />

objects and their parameters. Line 20 shows this second argument added to the<br />

NetStream.play( ) method.<br />

384 | Chapter 10: State Pattern<br />

Stop<br />

Append<br />

Record<br />

Play Pause

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

Saved successfully!

Ooh no, something went wrong!