Game Development Reference
In-Depth Information
// Assign a unique key
gs.setStateKey(""+m_id+dir);
gs.setStateType(GameConstants.GS_IS_UNIQUE);
// Set the properties
gs.setPieceId(m_id);
gs.setDir(dir);
// Add it to the room and distribute to
// other players
var gc:GameClient;
gc = PulseGame.getInstance().getGameClient();
gc.addGameState(gs);
}
The method creates the game state object, initializes the properties, and sends it to
the server. In this implementation, there are two important things to note: the first is
that all game states are unique, and the second is that they are added instead of just
broadcast to all the players. When a game state is added, the server persists the game
states until the game is over. This persistence allows players to join the game room
after the game has already started. The newly joined game player will first receive all
the game states that have been added by all players.
So what happens after the client sends the piece match game state to the server?
Since for each of our piece match game states we set a unique key property, the
server first checks if a previous game state with the same key was already added in
the game. If one was already found, the server rejects the new add request and sends
back a notification to the sender. If no previous game state was found with the same
unique key, then the game state is added to the persistent cache and it is broadcast
to all the players in the room. Note that the client who requested the addition of the
new game state will also receive back the game state.
Let's examine the code that handles the game state when it arrives at a client. The
notification is first handled by the main class and then passes the information to the
onPieceMatch method. This method simply gets the piece in question and then calls
_onPieceMatch method on it.
public static function onPieceMatch(pid:int, dir:int):void {
var p:PieceSprite;
p = getWithId(pid);
p._onPieceMatch(pid, dir);
}
The onPieceMatch will simply bring the piece and the correct piece in the direction
close enough so that they seem attached, and the groups of the two pieces are also
merged together.
 
Search WWH ::




Custom Search