Game Development Reference
In-Depth Information
//CHECK WALL COLLISIONS
var testReference:GridReference = getCollision
Reference(enemy);
var enemyRect:Rectangle = enemy.hitArea.getRect
(this);
var oldRect:Rectangle = enemyRect.clone();
enemyRect.offset(enemy.tempX - enemy.x, enemy.
tempY - enemy.y);
for each (var wall:IWall in testReference.walls) {
var wallRect:Rectangle = wall.getRect
(this);
var intersection:Rectangle = wallRect.
intersection(enemyRect);
if (!intersection.width || !intersection.
height) continue;
if (wallRect.right
= oldRect.left) {
//WALL IS TO THE LEFT
enemyRect.x += intersection.width;
enemy.motion.x *= -1;
<
}
if (wallRect.left > = oldRect.right) {
//WALL IS TO THE RIGHT
enemyRect.x -= intersection.width;
enemy.motion.x *= -1;
}
}
enemy.tempX = enemy.x + enemyRect.x - oldRect.x;
enemy.tempY = enemy.y + enemyRect.y - oldRect.y;
}
}
Each enemy has its own motion force, individually defined, and
each one is applied separately. In the same process, because we
re
already looping through the list of enemies, we test for wall collisions
using the getCollisionReference method we discussed earlier. If an
enemy is hitting a wall, its direction is reversed. For this example,
there is no accounting for physics on enemies, so gravity would not
affect them. However, it would not be terribly difficult to add support
for forces to be applied to enemies, as well as the player.
'
protected function checkPlayerCollisions():void {
var testReference:GridReference = getCollisionReference
(_player);
//CHECK INDIVIDUAL SPRITES
checkWalls(testReference);
checkItems(testReference);
checkEnemies(testReference);
}
Search Nedrilad ::




Custom Search