Game Development Reference
In-Depth Information
getRect method) and doing comparisons of intersection, overlap,
and so on. Although this doesn
t seem like it would be any better
than the hitTestObject method, it has a number of advantages. The
first is what I like to call predictive testing; basically, once you have
the rect of an object, you can move it around, scale it, and perform
point tests against it without any effect on the original object. In
order to test whether two objects are about to hit with the hitTest-
Object method, you must actually move the objects around, which
can occasionally cause glitches in the renderer. This is because
whenyouupdatetheposition,scale,orrotationofaDisplayObject
on the Stage, Flash will put it in the queue to redraw. By extracting
the rectangle first, you can do tests on it that don
'
'
t involve the dis-
play list at all and save the performance.
Another reason rect tests are a generally superior method of
detection is their greater flexibility. You can easily have multiple hit
areas on an object or determine how much two rectangles are
overlappingtodeterminetheforceofacollision.Let
'
ssayyou
have a vehicle that has multiple places in which it can take
damage. You could place Sprites (that would make themselves invi-
sible at runtime) to act as hit
so to speak. When you
needed to perform collision tests, you would iterate through these
sensors to get their rects. Once you have a set of rectangles, you
can test them individually or test them in combinations, using the
union method.
This next example will demonstrate rect testing by expanding on
a lesson from Chapter 7. Remember the SimpleShooter scolling
example? We
sensors,
ll take that base code and add enemies and collision
detection using rects. You can follow the example in the Simple-
ShooterCollisions.fla file and associated classes. There are two
main additions that have been made to the file since we last looked
at it: the new Enemy class and some method additions to the
SimpleShooterCollisions class.
'
The Enemy Class
public class Enemy extends MovieClip {
static public const FRAME_DESTROY:String = " destroy " ;
protected var _speed:Number;
protected var _alive:Boolean = true;
public function Enemy(speed:Number = 0) {
this.speed = speed;
stop();
}
Search Nedrilad ::




Custom Search