Code in frame 1 layer 1


//asteroids.fla

//By Andy Harris, Dummies Guide to Game Programming in Flash



init();



function init(){



  //normal initialization

  myShip.dx = 0;

  myShip.dy = 0;

  myShip.speed = 0;

  myShip.dir = 0;

  myShip.gotoAndStop("still");

  buildRocks();

} // end init



function buildRocks(){

  //rocks rely on rock.as class definition

  for(i = 0; i < 5; i++){

    _root.attachMovie("rock", "rock_" + i, 10 + i);

  } // end for loop

} // end buildRocks

 



myShip.onEnterFrame = function(){

  myShip.checkKeys();

  myShip.turn();

  myShip.move();

} // end if



myShip.checkKeys = function(){

  //check for left and right arrows

  if (Key.isDown(Key.LEFT)){

    this.dir -= 10;

    this.gotoAndStop("left");

    if (this.dir < 0){

      this.dir = 350;

    } // end if



  } else if (Key.isDown(Key.RIGHT)){

    this.dir += 10;

    this.gotoAndStop("right");

    if (this.dir > 360){

      this.dir = 10;

    } // end if

  } else if (Key.isDown(Key.UP)){

  //thrust on up arrow

    this.thrustSpeed = 1;

    this.gotoAndStop("thrust");

  } else {

    this.thrustSpeed = 0;

    this.gotoAndStop("still");

  } // end if

} // end checkKeys



myShip.turn = function(){

  this._rotation = this.dir;

  //trace("I'm here...");



  //get new thrust vector

  degrees = this.dir

  degrees -= 90;

  radians = degrees * Math.PI / 180;

  thrustDX = this.thrustSpeed * Math.cos(radians);

  thrustDY = this.thrustSpeed * Math.sin(radians);



  //add thrust to dx and dy

  this.dx += thrustDX;

  this.dy += thrustDY; 



} // end turn



myShip.move = function(){

  this._x += this.dx;

  this._y += this.dy;



  //wrap around screen

  if (this._x > Stage.width){

    this._x = 0;

  } // end if



  if (this._x < 0){

    this._x = Stage.width;

  } // end if



  if (this._y > Stage.height){

    this._y = 0;

  } // end if



  if (this._y < 0){

    this._y = Stage.height;

  } // end if



} // end move