title: Making falling objects come at random angles in AS3
body: <p>I am making a missile defense type of game and am trying to get the missiles to fall at random angles. I also need the bullet image to turn at the angle I am shooting at. I am very unfamiliar with angles in AS3 so I need some help.</p>
  
  <p>Code: 
  
          
  
  <pre><code>        import spark.components.Image;
  
          public var missiles:Array;
          public var bullets:Array;
          public var playerLife:Number;
  
          public var targetX:Number;
          public var targetY:Number;
  
          public function init():void {
              startGame();
          }
  
          public function onEnterFrame(e:Event):void { 
  
              if(Math.random() &lt;.05 ){
  
                  //make a new missle
                  var newMissile:Image = new Image();
  
                  //draw to is
                  newMissile.source = "assets/missileDown.jpg";
  
                  //position it
                  newMissile.x = Math.random() * stage.stageWidth;
  
  
                  //animate it
                  newMissile.addEventListener(Event.ENTER_FRAME, onMissileEnterFrame);
  
                  //add it to missle array
                  missiles.push(newMissile);
  
                  //add it to the screen
                  gameGroup.addElement(newMissile);
              }
          }
  
          public function startGame():void {
  
              //makes new arrays
              //gets rid of old arrays
              missiles = new Array();
              bullets = new Array();
  
              //set player life
              playerLife = 5;
  
              //show player life
              playerHealth.text = String(playerLife);
  
              //add animation and mouse interation
              this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
              stage.addEventListener(MouseEvent.CLICK, fireWeapon);
  
              //set game over alpha
              gameEnd.alpha = 0;
              reset.alpha = 0; 
  
              //set game start alpha
              playerHealth.alpha = 1;
              healthLabel.alpha = 1;
          }
  
          //updates the missle
          public function onMissileEnterFrame(e:Event):void {
  
              //reference to target
              var targetMissile:Image = Image(e.currentTarget);
  
              //move missle down
              targetMissile.y += 10;
  
              //if missle has gone too far, remove it and player loses life
              if(targetMissile.y &gt; stage.stageHeight) {
                  playerLife --;
                  removeMissile(targetMissile);
  
                  //show player life
                  playerHealth.text = String(playerLife);
              }
  
              //if player is dead, game over
              if(playerLife &lt;= 0) {
                  gameOver();
              }
  
          }
  
          //update bullet
          public function onBulletEnterFrame(e:Event):void {
  
              //get reference to bullet
              var thisBullet:Bullet = Bullet(e.currentTarget);
  
              //animate towards point..
  
              //calculate difference between current position and desired position
              var diffX:Number =  thisBullet.targX - thisBullet.x;
              var diffY:Number =  thisBullet.targY - thisBullet.y;
  
              //move 10% of difference closer
              thisBullet.x += diffX * .1;
              thisBullet.y += diffY * .1;
  
              //chekc for overlap between bullet and missles
              for(var i:Number = 0; i &lt; missiles.length; i++) {
  
                  //if they do overlap, remove missle
                  if( thisBullet.hitTestObject(missiles[i]) ) {
                      removeMissile(missiles[i]);
                      removeBullet(thisBullet);
                      break;
                  }
              }
  
              //if we're 'close enough' to the target position, remove bullet
              if(Math.abs(diffX) &lt;  10 &amp;&amp; Math.abs(diffY) &lt; 10) {
                  removeBullet(thisBullet);
              }
          }
  
          //gets rid of a missle
          public function removeMissile(targetMissile:Image):void {
  
              //removes the missle from the missiles array
              for(var i:Number = missiles.length - 1; i &gt;= 0; i--) {
                  if(missiles[i] == targetMissile) {
                      missiles.splice(i,1);
                      break;
                  }
              }
  
              //don't animate anymore
              targetMissile.removeEventListener(Event.ENTER_FRAME, onMissileEnterFrame);
  
              //remove from stage
              gameGroup.removeElement(targetMissile);
          }
  
          //removes bullet from stage
          public function removeBullet(targetBullet:Bullet):void {
  
              //stop animation
              targetBullet.removeEventListener(Event.ENTER_FRAME, onBulletEnterFrame);
  
              //remove from stage
              gameGroup.removeElement(targetBullet);
          }
  
          //shoot a bullet at the mouse position
          public function fireWeapon(e:MouseEvent):void {
  
              //make a new bullet
              var newBullet:Bullet = new Bullet();
              newBullet.addEventListener(Event.ENTER_FRAME, onBulletEnterFrame);
  
              //position near the earth in the center
              var halfStage:Number = stage.stageWidth / 2;
              newBullet.x = halfStage;
              newBullet.y = 500;
  
              //set target 
              newBullet.targX = stage.mouseX;
              newBullet.targY = stage.mouseY;
  
              //add it to the stage
              gameGroup.addElement(newBullet);
  
          }
  
          //you lose
          public function gameOver():void {
  
              //remove missles
              for(var i:Number = 0; i &lt; missiles.length; i++) {
                  removeMissile(missiles[i]);
              }
  
              //stop interaction
              stage.removeEventListener(MouseEvent.CLICK, fireWeapon);
  
              //stop animation
              this.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
  
              //set game start alpha
              playerHealth.alpha = 0;
              healthLabel.alpha = 0;
  
              //set game end alpha
              gameEnd.alpha = 1;
              reset.alpha = 1; 
          }
  
      ]]&gt;
  &lt;/fx:Script&gt;
  </code></pre>