Code in frame 1 layer 1

init();

function init(){
  gun.dir = 0;
  gun.charge = 0;
  bullet.dir = 0;
  bullet.speed = 0;
} // end init

_root.onEnterFrame = function(){
  checkKeys();
  move(bullet);
} // end enterFrame

function checkKeys(){
  if (Key.isDown(Key.LEFT)){
    gun.dir--;
    gun._rotation = gun.dir;
  } // end if

  if (Key.isDown(Key.RIGHT)){
    gun.dir++;
    gun._rotation = gun.dir;
  } // end if

  if (Key.isDown(Key.UP)){
    gun.charge++;
  } // end if

  if (Key.isDown(Key.DOWN)){
    gun.charge--;
  } // end if

  if (Key.isDown(Key.SPACE)){
    bullet._x = gun._x;
    bullet._y = gun._y;
    bullet.speed = gun.charge;
    bullet.dir = gun.dir;
    turn(bullet);
  } // end if

} // end checkKeys

function turn(sprite){
  //use vector projection to get DX and DY

  //offset the angle
  degrees = sprite.dir -90;

  //convert to radians
  radians = degrees / 180 * Math.PI;

  //get DX and DY (normalized: length is one)
  sprite.dx = Math.cos(radians);
  sprite.dy = Math.sin(radians);

  //compensate for speed
  sprite.dx *= sprite.speed;
  sprite.dy *= sprite.speed;
} // end turn;  

function move(sprite){
  //moves an object.  Kills it if it moves off stage

  sprite._x += sprite.dx;
  sprite._y += sprite.dy;

  if ((sprite._x > Stage.width) ||
      (sprite._x < 0) ||
      (sprite._y > Stage.height) ||
      (sprite._7 < 0)){
    //stop sprite and move it off stage
    sprite._x = -100;
    sprite._y = -100;
    sprite._speed = 0;
  } //end if
} // end move