ActionScript 3.0 Design Patterns

(Chris Devlin) #1

478 | Chapter 13: Symmetric Proxy Pattern


At this point, we’ll provide a quick overview of what each method does. In order to


get a mental image of what occurs, the functions are placed in the approximate order


of their launch, except for the last one, which is part of the housekeeping chore of


making sure that two players are connected to FMS prior to any move by either side.



  1. First, thenumConnect( )method checks to see how many clients are connected,
    and, if two are connected, it allows moves to be made by both players.

  2. The players select a move by pressing one of the three possible move buttons
    (Rock, Paper, or Scissors). Once a player selects a move, he presses a move but-
    ton that fires themakeMove( ) method.

  3. ThemakeMove( ) function fires both thedoMove( ) andlocalMove( ) methods.

  4. ThedoMove( ) operation calls the server to pass on the move to its proxy.

  5. ThelocalMove( )method first stores the move in a variable, and then sets a Bool-
    ean indicating the fact that the player has moved.

  6. Next, theonProxyMove( )function responds to the server call of a shared object
    and acts like thelocalMove( ) method, except it’s on the player’s proxy.

  7. When either thelocalMove( )oronProxyMove( )indicates that both players have
    moved, thetakeTurn( )method acts to force a call from theRefereeto deter-
    mine the winner and reset the values for a new game.


Some variation in the implementation of these methods determines whether moves


can be taken simultaneously or serially.


The Referee


Because the Referee class is abstract, it’s relatively small. The methods are fairly gen-


eral with the idea that they can be overridden; however, they must be purposely


developed. Further, because they’ll be placed in a template method, they have to be


Example 13-1. ISymPlayer.as


package
{
//Symetrical Proxy Interface
import flash.events.Event;


interface ISymPlayer
{
function numConnect(cl:uint):void;
function makeMove(event:Event):void;
function doMove(s:String):void;
function localMove(locMove:String):void;
function onProxyMove(proxMove:String):void;
function takeTurn( ):void;
}
}

Free download pdf