10.12.2012 Views

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Example 10-13. TestVid.as (continued)<br />

}<br />

}<br />

flv_txt.text="Provide file name";<br />

}<br />

}<br />

//Stop play<br />

private function doStop (e:MouseEvent):void<br />

{<br />

vidTest.stopPlay (ns);<br />

vid.clear ( );<br />

}<br />

Open a new Flash document file, and in the center of the stage, draw a rectangle<br />

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

FAB383. Use the Align panel to make sure that the rectangle is perfectly centered<br />

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

Document class window, type in TestVid, and test the application.<br />

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

can see the relationship between the video playing and the related trace statement<br />

showing what happens when you press the button. For example, if you press Start<br />

and the video is running, nothing new occurs because in the Play state, the<br />

startPlay( ) function does nothing other than offering a trace statement to the effect<br />

that you’re already playing.<br />

Expanding the State <strong>Design</strong>: Adding States<br />

A fundamental feature of virtually all design patterns is their ability to expand and<br />

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

some extent, the type of design pattern you select. In this particular application,<br />

we’re adding states.<br />

Adding the Pause State to the Statechart<br />

The first state to be added to the state machine is a Pause state. This state only exists<br />

inside the Play state, and you cannot get to the Pause state directly from the Stop state.<br />

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

Pause state on and off. To correctly depict this new state, we need to use a hierarchical<br />

state diagram. Figure 10-5 shows a statechart with the necessary hierarchy.<br />

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

states, and then, within the Play state are the Pause and No Pause states.<br />

Because the pause function is a toggle between the Play and Pause states, the No<br />

Pause state is exactly the same as the Play state. So, rather than creating Pause Start<br />

374 | Chapter 10: State Pattern

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!