Game Development Reference
In-Depth Information
The x coordinate is a random number between 800 (screen width) and 800 plus the
quadrant length. The reason to add 800 is to avoid the items from popping up on the
screen rather than loading when it's still not visible and brought into view as the ship
moves forward.
Detecting collisions
In this implementation, the collision is tested for ships colliding with asteroids only,
and not for other items. It should not be difficult to extend the behavior to all items.
The moveItems method responsible for moving the items at the ship's current speed
is called within the main game loop.
private function moveItems(q:Quadrant):void {
var items:Array = null;
var item:Item = null;
var i:int;
if ( q == null )
return;
items = q.getItems();
for ( i=0; i<items.length; i++ ) {
item = items[i] as Item;
item.update(m_ship.getSpeed());
if ( item.isAstroid() ) {
var a:Astroid;
a = item as Astroid;
m_radar.update(a);
//Check for astroid collisions
var ar:Rectangle;
var sr:Rectangle;
ar = a.getRect(this);
sr = m_ship.getRect(this);
if ( !a.isHit() && ar.intersects(sr) ) {
a.collide(m_ship);
}
}
}
}
The collision detection is simple; we get the sprite rectangle for the ship and each
of the asteroids and check for an intersection. If they intersect, we call the collide
method on the ship.
 
Search WWH ::




Custom Search