ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Expanding the State Design: Adding States | 377

Next, the newPauseStateclass implements theStateinterface. ThedoPause( )func-


tion in the Pause state uses the exact samens.togglePause( )used in thePlayState


class as shown in Example 10-17. However, instead of getting thePauseState, it gets


thePlayState. The interesting feature of a toggle method is that very different func-


tions may use the same class method.


11 trace("--Play State--");
12 this.videoWorks=videoWorks;
13 }
14 public function startPlay(ns:NetStream,flv:String):void
15 {
16 trace("You're already playing");
17 }
18 public function stopPlay(ns:NetStream):void
19 {
20 ns.close( );
21 trace("Stop playing.");
22 videoWorks.setState(videoWorks.getStopState( ));
23 }
24 public function doPause(ns:NetStream):void
25 {
26 ns.togglePause( );
27 trace("Begin pause.");
28 videoWorks.setState(videoWorks.getPauseState( ));
29 }
30 }
31 }

Example 10-17. PauseState.as


1 package
2 {
3 //Pause State #4
4 import flash.net.NetStream;
5
6 class PauseState implements State
7 {
8 var videoWorks:VideoWorks;
9 public function PauseState(videoWorks:VideoWorks)
10 {
11 trace("--Pause State--");
12 this.videoWorks=videoWorks;
13 }
14 public function startPlay(ns:NetStream,flv:String):void
15 {
16 trace("You have to go to unpause");
17 }
18 public function stopPlay(ns:NetStream):void
19 {
20 trace("Don't go to Stop from Pause");
21 }

Example 10-16. PlayState.as (continued)

Free download pdf