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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Because the application at this stage only provides traces, you will need to use the<br />

Flash Test (Control ➝ Test or Control ➝ Test Project) to see the output. Open a new<br />

Flash document, and, in the Document class window, type in TestState. The following<br />

should appear in the Output window:<br />

Video Player is On<br />

--Play State--<br />

--Stop State--<br />

Begin playing<br />

A new state is set<br />

You're already playing<br />

Stop playing.<br />

A new state is set<br />

You're already stopped<br />

Both the VideoWorks class and the PlayState and StopState classes include a trace<br />

statement to indicate their instantiation, and appear as soon as you test the script.<br />

Because the initial state is Stop, it changes states to the Play state when the first<br />

startPlay( ) method is invoked. Also, because you’re changing states, a trace statement<br />

from the VideoWorks class indicates a state change. However, when the second<br />

startPlay( ) method is invoked a second time, the same method in a different context<br />

recognizes that it’s already in the Play state and simply indicates that you’re<br />

already playing. When you press the Stop button, you move to the Stop state, setting<br />

a new state, but on the second press, the same function in this new context recognizes<br />

that you’re already in the Stop state, and indicates that fact.<br />

Video Player Concrete State Application<br />

All that you’ve seen so far has been the output as trace( ) statements to help understand<br />

how a State design pattern and State machine works. To add something useful,<br />

we need to include a reference to both a NetStream object and a string for<br />

referencing an FLV file. However, we need a string reference only for playing the<br />

video, because we can stop it simply by closing the NetStream instance. The following<br />

four scripts set up the state machine to actually play and stop a video. All of the<br />

trace statements have been left in place.<br />

With the implementation of an application that actually plays a video, we’ll need to<br />

import the necessary parts. Because the NetStream class is used in the interface and<br />

the two states, each of those files will need to import the class. However, while the<br />

VideoWorks class uses both the Play and Stop classes, it does not have to import the<br />

NetStream( ) class. This is because it’s already imported in the Play and Stop classes.<br />

The following five listings, Example 10-7 through Example 10-11, should be entered<br />

into an <strong>ActionScript</strong> file and saved with the captions as the filenames. Save all the<br />

files in the same folder.<br />

Video Player Concrete State Application | 367

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

Saved successfully!

Ooh no, something went wrong!