Game Development Reference
In-Depth Information
Figure 12.3 This car has two
hit points in the front and two
in the rear.
As Fig. 12.3 shows, you can use Sprites to visually mark the points
on the car, where you want to do a hit test. All you need to do then is
to have an identifier, separating the front ones from those in the back.
If the car backs into something solid, you want it to be able to drive
forward to pull away from it, but not to be able to back up any further.
Let
s look at a simple example of how this test can be used in prac-
tice. You can follow along in the HitTestPoint.fla file in the Chapter 12
examples folder. When you open up the FLA, you
'
ll find two objects
on the Stage: a square and two long rectangles. The square represents
our player character (and is named as such) and the rectangles are
part of the same clip called
'
Note that the square clip has a
number of dots along its outer border; these dots represent collision
test points. When the SWF is run, the square will move toward the
mouse at a given speed but will not be able to move past the barriers.
The code for this example is in three different classes: HitTestPoint.as,
HitTestCoordinate.as, and Player.as. We
barriers.
'
ll start with the Player.
public class Player extends Sprite {
private var _speed:int = 50;
private var _hitPointList:Vector.
<
HitTestCoordinate
>
;
public function Player() {
addEventListener(Event.ADDED_TO_STAGE, addedToStage,
false, 0, true);
}
private function addedToStage(e:Event):void {
_hitPointList = new Vector. < HitTestCoordinate > ();
for (var i:int = 0; i < numChildren; i++) {
var child:DisplayObject = getChildAt(i);
if (child is HitTestCoordinate)
_hitPointList.push(child);
}
}
Search Nedrilad ::




Custom Search