Game Development Reference
In-Depth Information
Putting items on the map
In order to have the items on the racetrack appear at the same position in all clients
and to have only the add operation succeed for each item, we will make use of
the unique game state. We will add one unique game state for each item on the
racetrack. Further, we will not add all the items on the race at once; we will do so
when it is actually needed, that is, when the quadrant is loaded. Each client during
the quadrant load adds a unique game state for each of the items. Only one client
will succeed in adding a unique game state because the key would be the same for
a particular item.
The items are held in a private array m_objects property. The items are added to the
array only after the client receives a successful (add) notification from the server. The
initItems is called during the load method of the quadrant. The initItems goes
into the loop of creating items, and before it does, it checks if m_objects has at least
one item. If there is, it means another client has already added the items or is in the
process of adding them—in this case, we simply return from the method.
When the new game state notification is received for the item objects, they are
delegated to the corresponding quadrant instance. The following illustration shows
the delegation from the main class AstroRace , which inherits from PulseGame
of PulseUI class:
Let's examine the onAddItem method of the RaceTrack object. After it calls the
onAddItem on the corresponding quadrant, the method checks if the quadrant
is already loaded; if so, the item is also added to the screen.
public function onAddItem(ic:ItemClient):void {
var q:int;
q = ic.getQuadrant();
var quadrant:Quadrant;
quadrant = m_quads[q] as Quadrant;
 
Search WWH ::




Custom Search