ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Information Shared Over the Internet | 483

TheRPSclass overrides thedoWinnermethod developed in theRefereeclass. The


other two methods in theRefereeclass are usable without any changes. A single


algorithm determines which side has won or if a tie occurred, and then transfers the


information into an array element that is then stored in a Stringvariable and


returned.


If you’re wondering whether it would be easier simply to make the
winnervariable a string and return it, you’re absolutely right. How-
ever, we liked the idea of being able to have a single array in one place
where you could add your own “smack” or “trash talk” to be dis-
played. So instead of simply displaying, “p1 Wins!” you could have
something like, “The Mighty Player 1 Conquers All!” Of course you
can be more creative than that.

When employing theRPSclass, we will observe the dictum to program to the inter-


face and not the implementation. So, in typing any instance where we would use the


RPSclass, we will type it asRefereeand instantiate it asRPS. (Look for this instantia-


tion of theRPS class in theSymPlayer1 andSymPlayer2 classes.)


Information Shared Over the Internet


At this point we need to take a little detour to discuss the techniques we used to send


data over the Internet for proxy work. As noted earlier in this chapter, we have chosen


to use Flash Media Server 2 to pass the move information from a player to its proxy


over the Internet. In order to see the relationships involved, we will show the purely


FMS2 server-side script and those portions of the player/proxy class that use it.


To begin, Example 13-4 shows theproxygame.ascfile (all lowercase). It is written in


ActionScript 1.0 because, at the time of this writing, all the code written for the


server-side could only be written in Server Side Communication ActionScript


(SSCA). The client-side counterpart is all ActionScript 3.0. This file sits in the server-


side location of the FMS2 host server. Generally, the host for the SWF files contain-


ing the compiled application and the server-side.ascfile are on the same host, but


not always. The connection to the server is through an RTMP protocol that’s part of


the client-side script.


gameOver=new Array("p1 Wins!","p2 Wins!","Tie!");
winNow=gameOver[winner];
return winNow;
}
}
}


Example 13-3. RPS.as (continued)

Free download pdf