package{ import flash.display.*; import flash.events.*; import flash.utils.*; import flash.geom.*; public class Game extends MovieClip{ //the bad guys private var enemies:Array; //the timer private var waveTimer:Timer; //the timer between private var newRoundTimer:Timer; //the current round startes out at 10 private var round:uint = 5; //the current enemy private var enemyIndex:uint = 0; //remember, this is the main function public function Game(){ this.enemies = new Array(); //temporary delay this.waveTimer = new Timer(1000); this.newRoundTimer = new Timer(1000); this.newRoundTimer.stop(); this.waveTimer.stop(); this.initRound(); } /** * This initializes a round of the game. */ public function initRound():void{ trace("init round"); this.enemies = new Array(); for(var i:uint = 0; i < this.round; i++){ //create a new badguy this.enemies[i] = new BadGuy(); this.enemies[i].x = 20 + 550 this.enemies[i].y = Math.ceil(Math.random() * 400); //add it to the stage, off to the right side addChild(this.enemies[i]); //stop its animation so that it doesn't start flying yet. this.enemies[i].stop(); } this.initWaveTimer(); } /** * This initializes the wave timer for sending wave after wave of enemies */ public function initWaveTimer():void{ trace('init wave'); this.waveTimer = new Timer(Math.round(Math.random() * 3000)); this.waveTimer.repeatCount = this.round + 1; trace(this.waveTimer.repeatCount); this.waveTimer.addEventListener(TimerEvent.TIMER, sendWave); this.waveTimer.start(); } /** * This sends a wave of enemies */ public function sendWave(e:Event):void{ if(!this.roundIsOver()){ this.waveTimer.delay = Math.round(Math.random() * 3000); this.enemies[this.enemyIndex].gotoAndPlay("fly"); this.enemies[this.enemyIndex].addEventListener(MouseEvent.CLICK, explode); this.enemyIndex++; }else{ this.waveTimer.removeEventListener(TimerEvent.TIMER, sendWave); this.waveTimer.stop(); trace ("grr"); this.round += 5; this.enemyIndex = 0; this.initNewRoundTimer(); } } public function explode(e:MouseEvent):void{ e.target.removeEventListener(MouseEvent.CLICK, explode); e.target.gotoAndPlay("explode"); e.target.updatePosition(e.target.mouseX); } public function roundIsOver():Boolean{ trace (this.enemyIndex + "+ " + this.round); return this.enemyIndex >= this.round; } public function initNewRoundTimer(){ trace ("nwe round timer"); this.newRoundTimer.repeatCount = 1; this.newRoundTimer.delay = 2000; this.newRoundTimer.addEventListener(TimerEvent.TIMER, startNewRound); this.newRoundTimer.start(); } public function startNewRound(e:Event){ this.newRoundTimer.removeEventListener(TimerEvent.TIMER, startNewRound); this.newRoundTimer.stop(); this.initRound(); } } }