Game Development Reference
In-Depth Information
ball and the center of the collision is used to
determine the angle of the collision.
Once the angle of collision has been deter-
mined, it is rounded to the nearest 90 degrees.
This is so that the appropriate offset can be cal-
culated to
Angle
theballandthewallapart.
This offset is then applied to the x and y coordi-
nates of the ball since the walls in this example
not to move in response to being hit.
private function checkHazards():void
{
push
Wall
Figure 15.7 When the ball and wall intersect, the
angle of the intersection is determined using the
center points of the ball and the collision.
var ballRect:Rectangle = _ball.getRect
(_currentLevel);
for each (var hazard:IHazard in _currentLevel.hazards)
{
if (hazard.getRect(_currentLevel).intersects
(ballRect))
{
resetBall();
break;
}
}
}
private function resetBall():void
{
_ball.x = _currentLevel.startPoint.x;
_ball.y = _currentLevel.startPoint.y;
_ball.vx = 0;
_ball.vy = 0;
}
Once the wall collisions have been resolved, the engine iterates
through all of the hazards in a level and determines if they inter-
sect. If the ball collides with any hazard, it is reset to initial starting
point with its velocity dropped to zero.
private function checkWin():void
{
var testPoint:Point = new Point(_ball.x, _ball.y);
if (Point.distance(testPoint, _currentLevel.endPoint) <
DISTANCE_TO_ENDPOINT)
{
endLevel();
}
}
The final method of the class, checkWin ,simplymeasuresthe
distance between the ball and the endpoint specified by the level.
Search Nedrilad ::




Custom Search