In the tic-tac-toe and 9-ball chapters we discussed all of the basic multiplayer techniques that are used in Flash multiplayer games:
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:
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. |