ActionScript 3.0 Design Patterns

(Chris Devlin) #1

378 | Chapter 10: State Pattern


As with the individual state classes, you also need to add aPauseStateinstance to the


VideoWorksclass as shown in Example 10-18. Remember that this class contains all


the setters and getters, and you certainly need that to invoke the pause behavior for


the application. This is easy to do because all it takes is a single variable for the


PauseState instance, adoPause( ) function, and then adding the pause getter.


22 public function doPause(ns:NetStream):void
23 {
24 ns.togglePause( );
25 trace("Quit pausing.");
26 videoWorks.setState(videoWorks.getPlayState( ));
27 }
28 }
29 }

Example 10-18. VideoWorks.as


package
{
//Context Class #5
import flash.net.NetStream;


class VideoWorks
{
private var playState:State;
private var stopState:State;
private var pauseState:State;
private var state:State;
public function VideoWorks( )
{
trace("Video Player is on");
playState = new PlayState(this);
stopState = new StopState(this);
pauseState = new PauseState(this);
state=stopState;
}
public function startPlay(ns:NetStream,flv:String):void
{
state.startPlay(ns,flv);
}
public function stopPlay(ns:NetStream):void
{
state.stopPlay(ns);
}
public function doPause(ns:NetStream):void
{
state.doPause(ns);
}
public function setState(state:State):void
{
trace("A new state is set");


Example 10-17. PauseState.as

Free download pdf