import flash.events.MouseEvent; // add event listener to rotate cannon when mouse moves stage.addEventListener(MouseEvent.MOUSE_MOVE, rotateCannon); // add event listener to call fireBullet function on each mouse click stage.addEventListener(MouseEvent.CLICK, fireBullet); // add event listener to execute moveBullet each time frame 1 repeats stage.addEventListener(Event.ENTER_FRAME,moveBullet); var xDiff, yDiff:Number; var radians:Number; var degrees:Number; var speed:int = 5; function moveBullet(evt:Event):void { // only execute if we have some children // of bulletContain, in this case bullets) if(bulletContainer.numChildren > 0) { // loop through all bullets for(var b = 0; b<=bulletContainer.numChildren - 1; b++) { // references each child and assigns value to tempBullet var tempBullet = bulletContainer.getChildAt(b); // add movement from fireBullet function newBullet.xMov & newBullet.yMov tempBullet.x += tempBullet.xMov; tempBullet.y += tempBullet.yMov; if(tempBullet.x < 0 || tempBullet.x > stage.stageWidth || tempBullet.y < 0 || tempBullet.y > stage.stageHeight) { // remove in opposite order that bullet was added // removing bullet from bulletContainer bulletContainer.removeChild(tempBullet); trace(bulletContainer.numChildren); } if(tempBullet.hitTestObject(target)) { bulletContainer.removeChild(tempBullet); } } } } function fireBullet(evt:MouseEvent):void { //get "bullet" from library linkage constructor var newBullet:bullet = new bullet(); // start position of newBullet at end of cannon newBullet.x = cannon.x + Math.cos(radians) * 50; newBullet.y = cannon.y + Math.sin(radians) * 50; // move along x-axis newBullet.xMov = Math.cos(radians) * speed; // move along y-axis newBullet.yMov = Math.sin(radians) * speed; //In action script the addChild draws to screen //making it a child of bulletContainer instance // Timeline tab > select bullets layer > hide other layers // > highlight object & check Properties bulletContainer.addChild(newBullet); } function rotateCannon(evt:MouseEvent):void { xDiff = mouseX - cannon.x; yDiff = mouseY - cannon.y; radians = Math.atan2(yDiff, xDiff); degrees = radians * 180 / Math.PI; cannon.rotation = degrees; rotationAmount.text = String(Math.round(degrees) + " degrees"); //drawLine(); }