Game Development Reference
In-Depth Information
_enemyGenerator.addEventListener(TimerEvent.TIMER,
createEnemy, false, 0, true);
}
protected function addedToStage(e:Event):void {
_stageWidth = stage.stageWidth;
_stageHeight = stage.stageHeight;
addEventListener(MouseEvent.MOUSE_DOWN, createProjectile,
false, 0, true);
_enemyGenerator.start();
}
protected function frameScript(e:Event):void {
movePlayer();
moveProjectiles();
moveEnemies();
checkCollisions();
moveForeground();
moveBackground();
}
In the initialization functions, there are now variables for how
frequently enemies are generated, how fast they move, and a
Timerobjecttocreatethem.Intheframeloop,therearealsotwo
new methods called which we will look at next.
protected function moveEnemies():void {
for each (var enemy:Enemy in _enemyList) {
enemy.x += enemy.speed;
if (enemy.x + enemy.width < 0) {
removeEnemy(enemy);
}
}
}
protected function createEnemy(e:TimerEvent = null):void {
var enemy:Enemy = new Enemy(_enemySpeed);
enemy.x = _stageWidth + enemy.width;
enemy.y = Math.random() * (_stageHeight enemy.height) +
(enemy.height/2);
addChild(enemy);
_enemyList.push(enemy);
}
protected function removeEnemy(enemy:Enemy):void {
if (enemy.parent == this) removeChild(enemy);
_enemyList.splice(_enemyList.indexOf(enemy),1);
}
Search Nedrilad ::




Custom Search