Asteroids Game Stage 3 – Creating and Moving Rocks

{loadposition adposition4}

{loadposition adposition5}Stage 3 – Creating and Moving Rocks

In stage 3 of the Asteroids game development process, you will create the asteroids that the spaceship will need to eliminate in order to get to higher levels. To create multiple asteroids, you will use a while loop, and to keep track of the asteroids you will create an array. The necessary graphics for stage 3 consists of an asteroid movieclip with the linkage identifier set to asteroid.

The game code and video tutorials below are specific to stage 3, initializing and moving the asteroids on the game screen. 

Stage 3 Game Code (Stage 3 code is highlighted in red):

stop();
init();

var NUMSHOTS:Number = 0;
var SHOTINDEX:Number = 0;
var SHOTTIMECOUNTER:Number = 5;

function init(){
    SCORE = 0;
    LIVES = 3;
    MAXAST = 3;
    SHIPDEAD = false;
    LEFT = 0;
    TOP = 0;
    BOTTOM = Stage.height;
    RIGHT = Stage.width;
   
    ast_array = new Array();
    createEmptyMovieClip("astField_mc", 0);

    createEmptyMovieClip("shots_mc", 1);
   
    astInit();
    shipInit();
}

function astInit(){
    ASTINDEX = 0;
    while(ASTINDEX < MAXAST){
        createAst();
    }
}

function shipInit(){
    attachMovie("ship", "ship_mc", 2);
    ship_mc._x = RIGHT/2;
    ship_mc._y = BOTTOM/2;
    ship_mc.VELX = 0;
    ship_mc.VELY = 0;
    ship_mc.onEnterFrame = control;
    ROTATE = 0;
    THRUST = 0;
}

function control(){
    if(SHIPDEAD == false){
        if(Key.isDown(Key.LEFT)){
            ROTATE = -1;
        }else if(Key.isDown(Key.RIGHT)){
            ROTATE = 1;
        }else{
            ROTATE = 0;
            ship_mc.gotoAndStop(1);
        }
        if(Key.isDown(Key.UP)){
            THRUST = .1;
            ship_mc.gotoAndStop(2);
        }else{
            THRUST = 0;
            ship_mc.gotoAndStop(1);
        }
        if(Key.isDown(68)){
            SHOTTIMECOUNTER++;
            if(SHOTTIMECOUNTER == 6){
                shoot();
                SHOTTIMECOUNTER = 0;
            }
        }
    }
    move();
}

function move(){
    if(ROTATE){
        ship_mc._rotation += ROTATE*3;
    }
    if(THRUST){
        RAD = ship_mc._rotation*Math.PI/180;
        ship_mc.THRUSTX = Math.cos(RAD)*THRUST;
        ship_mc.THRUSTY = Math.sin(RAD)*THRUST;
        ship_mc.VELX += ship_mc.THRUSTX;
        ship_mc.VELY += ship_mc.THRUSTY;
    }
    ship_mc._x += ship_mc.VELX;
    ship_mc._y += ship_mc.VELY;
   
    if(ship_mc._x > RIGHT){
        ship_mc._x = LEFT;
    }else if(ship_mc._x < LEFT){
        ship_mc._x = RIGHT;
    }else if(ship_mc._y > BOTTOM){
        ship_mc._y = TOP;
    }else if(ship_mc._y < TOP){
        ship_mc._y = BOTTOM;
    }
}

function shoot(){
    if(NUMSHOTS < 5){
        NUMSHOTS++;
        shot_mc = shots_mc.attachMovie("shot","s"+SHOTINDEX, SHOTINDEX++);
        shot_mc.onEnterFrame = shotMove;
       
        RAD = ship_mc._rotation*Math.PI/180;
        shot_mc._x = ship_mc._x+10*Math.cos(RAD);
        shot_mc._y = ship_mc._y+10*Math.sin(RAD);
       
        shot_mc.VELX = 3*Math.cos(RAD)+ship_mc.VELX
        shot_mc.VELY = 3*Math.sin(RAD)+ship_mc.VELY
    }
}

function shotMove(){
    this._x += this.VELX;
    this._y += this.VELY;
   
    if(this._x > RIGHT ||
       this._x < LEFT ||
       this._y > BOTTOM ||
       this._y < TOP)
    {
        removeMovieClip(this);
        if(NUMSHOTS > 0){
            NUMSHOTS–;
        }
    }
}

function createAst(){
    ast_mc = astField_mc.attachMovie("asteroid","ast"+ASTINDEX,ASTINDEX++);
    ast_mc._rotation = Math.random()*360;
    ast_mc.VELX = Math.random()*3-2;
    ast_mc.VELY = Math.random()*3-2;
    ast_mc.onEnterFrame = astMove;
    ast_array.push(ast_mc);
}

function astMove(){
    this._x += this.VELX;
    this._y += this.VELY;
    if(this._x > RIGHT){
        this._x = LEFT;
    }else if(this._x < LEFT){
        this._x = RIGHT;
    }else if(this._y > BOTTOM){
        this._y = TOP;
    }else if(this._y < TOP){
        this._y = BOTTOM;
    }

 

{loadposition adposition9}

Video Tutorials

In part 7, I show how to use a while loop, an attachMovie function, and an array
to create multiple asteroids and have a method to keep track of them 

 

In part 8, I show how to move the asteroids in random directions

 

{loadposition adposition8}

Author: Dan

Dan teaches computer networking and security classes at Central Oregon Community College.

Leave a Reply

Your email address will not be published. Required fields are marked *