package {
import com.adobe.viewsource.ViewSource;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
[SWF(width="550", height="400", backgroundColor="#E0FAFE", frameRate="30")]
public class MissileExample extends Sprite {
private var frameCount:uint = 0;
private var cannon:Cannon;
private var missileTarget:MissileTarget;
public function MissileExample() {
addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}
private function addedToStageHandler(event:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
init();
}
private function init():void {
cannon = new Cannon();
cannon.x = 30;
cannon.y = stage.stageHeight - 30;
addChild(cannon);
missileTarget = new MissileTarget();
missileTarget.x = 250;
missileTarget.y = 200;
addChild(missileTarget);
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
ViewSource.addMenuItem(this, "http://www.nbilyk.com/examples/missileExample/srcview/index.html");
}
private function enterFrameHandler(event:Event):void {
cannon.rotation = Math.atan2(mouseY - cannon.y, mouseX - cannon.x) * 180 / Math.PI - 90;
frameCount++;
}
private function mouseDownHandler(event:MouseEvent):void {
var missile:Missile = new Missile(missileTarget);
missile.x = cannon.x;
missile.y = cannon.y;
missile.velocity = Point.polar(30, (cannon.rotation + 90) * Math.PI / 180);
addChild(missile);
}
}
}