ActionScript 3.0 Design Patterns

(Chris Devlin) #1

374 | Chapter 10: State Pattern


Open a new Flash document file, and in the center of the stage, draw a rectangle


(W=320, H=240) with a 6-point stroke, the color value 9E0039, and the fill color


FAB383. Use the Align panel to make sure that the rectangle is perfectly centered


because it serves as a backdrop for the video that will be placed on top of it. In the


Document class window, type inTestVid, and test the application.


The UI is simple and related to the transitions—Stop and Start (playing video).You


can see the relationship between the video playing and the relatedtracestatement


showing what happens when you press the button. For example, if you press Start


and the video is running, nothing new occurs because in the Play state, the


startPlay( )function does nothing other than offering atracestatement to the effect


that you’re already playing.


Expanding the State Design: Adding States


A fundamental feature of virtually all design patterns is their ability to expand and


accept change. The kind of change you’re expecting in an application determines, to


some extent, the type of design pattern you select. In this particular application,


we’re adding states.


Adding the Pause State to the Statechart


The first state to be added to the state machine is a Pause state. This state only exists


inside the Play state, and you cannot get to the Pause state directly from the Stop state.


To get to the Pause state, you must first be in the Play state, and then you can turn the


Pause state on and off. To correctly depict this new state, we need to use a hierarchi-


cal state diagram. Figure 10-5 shows a statechart with the necessary hierarchy.


The hierarchy in Figure 10-5 is a simple one. The first level is the Play and Stop


states, and then, within the Play state are the Pause and No Pause states.


Because the pause function is a toggle between the Play and Pause states, the No


Pause state is exactly the same as the Play state. So, rather than creating Pause Start


flv_txt.text="Provide file name";
}
}
//Stop play
private function doStop (e:MouseEvent):void
{
vidTest.stopPlay (ns);
vid.clear ( );
}
}
}


Example 10-13. TestVid.as (continued)

Free download pdf