Multiplayer Actions


In the tic-tac-toe and 9-ball chapters we discussed all of the basic multiplayer techniques that are used in Flash multiplayer games:

  • Creating room variables on the server to keep track of when both players have arrived and when a player has left the game.

  • Sending a move to your opponent and creating a property called type, which specifies the type of move being made, on the object you send to your opponent.

  • Joining the game room at the right time.

  • Creating room variables on the server to keep track of when both players' game instances are ready for the next move. (See the "Synchronization: A Multiplayer Problem and Solution" section of Chapter 18, "9-Ball," for details on this.)

If you have not yet read Chapter 17, "Tic-Tac-Toe: Your First Multiplayer Game," you should probably do so now, as we are not going to go over these topics again here. However, we will briefly review what is happening on the multiplayer side of this game and mention a few pieces of the multiplayer code that are specific to this game (such as the information sent in a sendMove() action).

Return to the main stage of dont_fall.fla. As with the other multiplayer games discussed, this file is made from the original chat file dissected in Chapter 13, "Dissecting a Chat," with the difference between this file and the original chat file being the contents of the Game frame. On that frame, we have some chat ActionScript and a movie clip that contains all of the game assets.

Click the Game frame in the Game Chat Actions layer. Open the Actions panel. The ActionScript you see in this frame is used to handle the chatting that can happen during the game. It is exactly the same ActionScript used for the game chat in tic-tac-toe and 9-ball.

Also in the Game frame you'll find a movie clip that contains all of the game assets and game ActionScript. Double-click this movie clip to view the contents. You'll see four layers: Multiplayer Actions, Iso Object, Game Actions, and Assets. The only layer we need to concern ourselves with here is Multiplayer Actions.

Click the Multiplayer Actions layer and open the Actions panel. This ActionScript is very similar to the ActionScript found in the Multiplayer Actions layer of 9-ball. When this frame is first visited, we do the following:

  1. Define the event handlers for moveReceived and onRoomVarChange.

  2. Join the game room.

  3. Set a room variable that flags a player as being in the room and ready to play.

The onRoomVarChanged function is called when a room variable is created, modified, or deleted. Room variables contain information telling us if both players are in the room and if they are both ready to receive a move. If they are both in the room, and this is the first time we have determined that they are both in the room, then we call the startGame() function. If they are both ready to move, then we unlock the game by setting locked = false.

The sendMove() function sends whatever object we pass in from the current player to the opponent. The moveReceived() function is then called in the opponent's game instance. Here is the moveReceived() function:

 1   function moveReceived(ob) { 2      if (ob.type == "move") { 3         clickAccepted(ob.xm, ob.zm, ob.x_cell, ob.z_cell); 4      } else if (ob.type == "restart") { 5         restart(); 6      } 7   } 

As you can see from this function, there are only two types of moves an actual game move and a restart move. When a move of type move is received, four variables are extracted from the object and passed into the clickAccepted() function. These values describe the end position of where the character should walk to, and the row and column number of the end tile. They will be discussed more in the "Game Actions" section.

There are no multiplayer surprises in this chapter. All of the techniques used have been seen in the previous chapters. The code discussed here is almost identical to what you have seen before.



Macromedia Flash MX Game Design Demystified(c) The Official Guide to Creating Games with Flash
Macromedia Flash MX Game Design Demystified: The Official Guide to Creating Games with Flash -- First 1st Printing -- CD Included
ISBN: B003HP4RW2
EAN: N/A
Year: 2005
Pages: 163
Authors: Jobe Makar

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net