Game Development Reference
In-Depth Information
As it passes the current quadrant and enters the next one, we unload the quadrant
that the ship crossed over. When a quadrant object is loaded, it also adds all the
items to the screen. Note that all items may not be immediately visible on the screen.
When the quadrant is unloaded, all items belonging to it will also be removed from
the display list. This technique will help us create tracks of arbitrary length without
compromising the performance.
All quadrant objects are created in the constructor of the racetrack object.
public function RaceTrack(stage:Stage, pulse:GameClient) {
// Create all quadrant objects
var i:int;
for ( i=0; i<QUADRANT_COUNT; i++ ) {
var q:Quadrant = new Quadrant(i, i*QUADRANT_LENGTH);
m_quads.push(q);
}
}
We increment the ship's universe position as the ship moves forward based on its
current speed. Since we know the length of each quadrant, we can determine in
which quadrant the ship is currently in. Within the game loop, we load and
unload quadrants as the ship progresses through the track.
private function gameLoop(event:TimerEvent):void {
var cqi:int = m_ship.getCoord()/QUADRANT_LENGTH;
if ( cqi >= QUADRANT_COUNT ) {
// Reached the end of race, unload
// all quadrants
unloadQuadrant(m_quads[cqi-1] as Quadrant);
unloadQuadrant(m_quads[cqi] as Quadrant);
unloadQuadrant(m_quads[cqi+1] as Quadrant);
}
// Load and unload quadrant data
loadQuadrant(m_quads[cqi] as Quadrant);
 
Search WWH ::




Custom Search