Game Development Reference
In-Depth Information
Really, the only code happening in this class is in the
enterFrame method. It measures the distance between the mouse
and the player. If it is less than the speed of the player in a single
frame, the player attempts to move to the mouse
sexactposition
(this is to prevent the player from eternally jumping back and forth
over the mouse). If it is further away, the player will calculate its
angle relative to the mouse and then move at its given speed in
that direction. However, before the new coordinates are assigned,
they are stored in a point object, tempPoint .A for each loop then
iterates through every coordinate in the player
'
s list. It checks these
coordinates, adjusted for the change in position, against the bar-
riers clip. If it detects a collision, then it checks the individual x
and y values to determine the direction in which the collision is
occurring.
If you noticed the position of the test points in the player Sprite,
you noted there are a total of eight, one for each side and one for
each corner. The distance between them is such that you can actu-
ally coerce the square onto the barrier walls, as they are thin
enough to fit between the points. Although it looks like a bug, I left
this behavior in to make a point (no pun intended). Even if you
find a technique that works for you, you will probably have to
make some adjustments as you test. In this case, because we
'
re
dealingwithsuchthinbarriers,we need to position the collision
points closer together and probably have more of them. By making
these essentially little components, it is very easy to adjust the
number and positioning of these points; remember that they only
need to be slightly closer together than the smallest object you
'
re
testing against. That said, you might have a game where you need
to wrap one object around another in which case the current
behavior would be ideal.
'
Radius/Distance Testing—Great for Circles
Although not an actual method of DisplayObjects, a very accurate
way of detecting collision between two circular objects (or a circular
object and a point) is simply by using the distance formula. If you
know the radius of each object you want to test against each other,
you can add the two radii together and see if it is greater than the
distance between them. In addition to flat, two-dimensional circles,
this method works very well for characters on an isometric, or
angled, playfield.
In Fig. 12.5 , there are two characters with each having a radius
of
Atraditional hitTestObject would not work
here because the objects will visually overlap when one passes in
front of another. Instead, we need to measure the distance between
the two players and determine if they are close enough to be
personal space.
Search Nedrilad ::




Custom Search