ActionScript 3.0 Design Patterns

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

and Pause Stop functions, a “Do Pause” behavior will be established to act differ-


ently in different states. In a Pause state, the Do Pause behavior returns to the default


Play state and in the Play state, it goes to the Pause state.


Adding New Behaviors


The first step is to add a new behavior (method) to the current ones in theState.as


file. This behavior is a state transition from playing to pause and back to playing. To


begin, the first element to add is the pause behavior to the set of states. This is fairly


simple because it too has aNetStreamparameter, so changes can be made without


further imports:


function doPause(ns:NetStream):void;


The first step is to change theState.asby adding the new function as shown in


Example 10-14 line 9:


One of the requirements for an interface is that all implementations of the interface


include all the interface methods. The next step will be to add the method to the


Stop and Play states as in Example 10-15:


Figure 10-5. Hierarchical statechart


Example 10-14. 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 stopPlay(ns:NetStream):void;
9 function doPause(ns:NetStream):void;
10 }
11 }

Example 10-15. StopState.as


1 package
2 {
3 //Stop State #2
4 import flash.net.NetStream;
5

Stop Play Pause
Free download pdf