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.

three moves has been selected, or if the Move button is clicked. To simplify matters,<br />

all name properties are lowercase while the label properties begin with an uppercase<br />

letter. (e.g., Rock=label, rock=name). The Move button is included to let the user<br />

change her mind after selecting one of the three moves. By clicking the Move button,<br />

the player <strong>com</strong>mits to the selected move by launching two methods, doMove( )<br />

and localMove( ).<br />

Dual moves<br />

The function to actually make a move once its been selected by clicking the Move<br />

button sends the move to the proxy using doMove( ), and stores it locally in a variable<br />

with localMove( ). This is the heart of the Symmetric Proxy. The symmetry lies<br />

in the fact that all moves are sent to both the local and proxy players.<br />

The local move checks to be sure that the local player has not moved yet, and then<br />

checks to see if the other side has moved. If the other side has moved, then the information<br />

about the moves is turned over to the Referee to determine and display the<br />

out<strong>com</strong>e.<br />

Now the doMove( ) is really nothing more than a call to the server-side script. It passes<br />

the move to the server and does no more.<br />

Proxy move<br />

The onProxyMove( ) method is the mirror image of the localMove( ). Because the<br />

method is fired no matter which player moves, the algorithm in the method first filters<br />

out the local move by splitting the string returned from the server into a move and a<br />

player name. It looks at the player name, and if it’s the local’s name, it ignores it and<br />

the move associated with it. (The move is handled by the localMove( ) method.)<br />

However, once the filtering has been <strong>com</strong>pleted, the algorithm is essentially the same<br />

as the local move operation. It does display the fact that the opponent has moved by<br />

displaying the name of the player but not the move. In this way, the application maintains<br />

the simultaneous nature of the play.<br />

Referee object<br />

The end game conditions are delegated to the Referee object. In this case, the Referee<br />

object is the RPS subclass contained within the takeTurn( ) method. By passing the<br />

moves, output text field, and name of the array used to keep track of who took a turn<br />

to the RPS object’s moveComplete( ) template method, everything can be neatly<br />

wrapped up. The single line,<br />

rps.moveComplete(p1move,p2move,playerText,monitor);<br />

takes advantage of the template method in the Referee class and launches the operations<br />

that make up the template method—doWinner( ), displayResults( ), and<br />

resetGame( ).<br />

492 | Chapter 13: Symmetric Proxy Pattern

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

Saved successfully!

Ooh no, something went wrong!