ActionScript 3.0 Design Patterns

(Chris Devlin) #1

376 | Chapter 10: State Pattern


ThedoPause( )method is added to theStopState.asfile as shown in line 24, but it


makes no sense to set the Pause state from the Stop state because then nothing is


playing to pause. So all that’s added is atrace( )statement on line 26 that indicates


that such a transition is not possible.


Working with the Play state is an entirely different proposition. Essentially, the Pause


state is a subset of the Play state. In fact, when using a toggle pause, you can think of


the play as “play without pause” and “play with pause.” With ActionScript 3.0,


another option is to use the newNetStream.pause( )andNetStream.resume( )meth-


ods. Had we done that, two, instead of one, additional states would be required. For


now, though, just change thePlayState.asfile as shown in Example 10-16, in lines


24–29:


6 class StopState implements State
7 {
8 private var videoWorks:VideoWorks;
9 public function StopState(videoWorks:VideoWorks)
10 {
11 trace("--Stop State--");
12 this.videoWorks=videoWorks;
13 }
14 public function startPlay(ns:NetStream,flv:String):void
15 {
16 ns.play(flv);
17 trace("Begin playing");
18 videoWorks.setState(videoWorks.getPlayState( ));
19 }
20 public function stopPlay(ns:NetStream):void
21 {
22 trace("You're already stopped");
23 }
24 public function doPause(ns:NetStream):void
25 {
26 trace("Cannot go to Stop from Pause.");
27 }
28 }
29 }

Example 10-16. PlayState.as


1 package
2 {
3 //Play State #3
4 import flash.net.NetStream;
5
6 class PlayState implements State
7 {
8 private var videoWorks:VideoWorks;
9 public function PlayState(videoWorks:VideoWorks)
10 {

Example 10-15. StopState.as (continued)

Free download pdf