Game Development Reference
In-Depth Information
removeChild(states[i]);
}
m_gs.reset();
if ( m_addBtn.parent != null )
removeChild(m_addBtn);
if ( m_removeBtn.parent != null )
removeChild(m_removeBtn);
super.hide();
}
Notice that along with the needed sprite for the buttons, we also keep track of the
game state sprites in a hash map ( m_gs ), as well as keeping track of the colored circle
that was last clicked on ( m_selected ).
Another important method to override is the start method, which is called when the
host starts the game. Since in this sample we allow the players to enter even after
the game has started, the show method does a check if the room status is already in
playing state. If so, then the add and remove buttons are drawn.
public override function startGame():void {
super.startGame();
// Game started, add the buttons
addChild(m_addBtn);
addChild(m_removeBtn);
}
As we saw in the implementation of the main class, there were three methods that
would be called when we received a new game state, when a game state was removed,
and when it was modified. The implementation of this is simply delegated to this game
screen. Now let us examine the code to see what happens in each of the three cases.
When a new game state is added, we create a new game state sprite and add it to
the screen. After creating the game state sprite, we add it to the map so that we can
access it later during removal or update. We also hook up the mouse event listeners
so that we can implement the drag and selection capabilities. Finally, we set the
x and y coordinates for the colored circle from the values found in the game state
object and add it to the screen.
private function
newGameState(gameState:HelloGameStateClient):void {
// Create and show a corresponding
// sprite for the game state
var s:Sprite = new GameStateSprite(gameState);
m_gs.put(gameState.getId(), s);
s.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
s.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
s.x = gameState.getX();
s.y = gameState.getY();
 
Search WWH ::




Custom Search