Game Development Reference
In-Depth Information
// Get the game state that is represented by this sprite
var gs:HelloGameStateClient;
gs = sprite.m_gs;
if ( gs != null ) {
gs.setX(sprite.x);
gs.setY(sprite.y);
PulseGame.getInstance().
getGameClient().updateGameState(gs);
}
setSelected(sprite);
}
As in the case of handling the adding of game state, we will update the game state
sprite after we receive the update notification from the server.
So in the HelloGame, we override the callback method as follows:
public override function
onUpdateGameState(gameState:GameStateClient):void {
(m_gameScreen as HelloGameScreen).onUpdateGS(gameState);
}
The method onUpdateGS is listed below:
public function
onUpdateGS(gameState:GameStateClient):void {
// An update game state was received from server
var gs:HelloGameStateClient;
gs = gameState as HelloGameStateClient;
var s:GameStateSprite = m_gs.getValue(gs.getId());
if ( s == null ) {
// This can happen when the player
// joins in the middle of the game
newGameState(gs);
}
else {
s.x = gs.getX();
s.y = gs.getY();
}
}
During an update, we get the sprite that the game state is represented by and update
the position based on the received game state. We first get the sprite based on the ID
value of the received game state. Note that we need to handle a special case. If the
received game state did not have a corresponding sprite, we would need to create it.
This should not happen normally, but could happen for a client that has entered the
room after a few game states were already added by another client in the room.
 
Search WWH ::




Custom Search