Game Development Reference
In-Depth Information
their
distance
from the player (i.e., move closer or further away
from the player
re likely going to have to deal with
managing the indices of these objects. If you detect that two objects
are touching, you have a great opportunity to check their positions
and display indices.
'
s perspective), you
'
HitTestPoint—One Step Up
In earlier versions of Flash, hitTestObject and its counterpart,
hitTestPoint ,werebothpartofthesamemethod hitTest .InAS3,
Adobe broke the two up into discrete methods, both for speed and
for accurate type checking. Unlike the object version of this
method, hitTextPoint accepts x and y coordinates to check if the
DisplayObject is overlapping a particular pixel. In fact, when testing
against objects that have empty space (not transparent image data
like an alpha channel, but actually void of data), this method has
the option of accurately telling you if the shape is overlapping the
point. Obviously, this method is considerably more accurate than
hitTestObject , but it only does a single point in space. To test a
complex shape against another, you
'
dneedtodothistestmany
times at points all around the shape
s outer border. This would
quickly become taxing for the processor, particularly if there are
multiple objects colliding on screen. It is most commonly used
when determining whether the mouse coordinates are overlapping
a particular shape.
One thing that is important to note about this method is that it
expects to receive its coordinates as they would appear on the
Stage. If you are testing against a point embedded in several
DisplayObjects in the display list and their coordinate systems do
not line up with the Stage, then you
'
'
ll need to convert the coordi-
nates to the Stage
s system. Luckily, all DisplayObjects give you a
method to do this, called localToGlobal .Itacceptsapointobject
and converts it numerically to the Stage coordinate system.
'
var clip1:Sprite = new Sprite();
clip1.x = clip1.y = 50;
var testPoint:Point = new Point(0, 0);
testPoint = clip1.localToGlobal(testPoint);
trace(testPoint); //OUTPUTS X = 50, Y = 50
In this short snippet, a Sprite is created on the Stage and has its
coordinates set to (50, 50). According to the Sprite
s coordinate system,
its center is at (0, 0). By running localToGlobal on the point object, we
can see that according to the Stage, the center is actually at (50, 50).
Another good use for this method is when doing hit tests for
vehicles against scenery. You can use a pair of points for the two
front bumper ends and a pair for the rear.
'
Search Nedrilad ::




Custom Search