Asteroids Game Stage 2 – Moving Projectiles

{loadposition adposition4}

Stage 2 – Moving Projectiles

In stage 2 of the Asteroids game development process, you will create the projectile movieclip that the ship will fire. {loadposition adposition5}The goal is to write Actionscript code that will attach the projectile to the stage, place it in the correct position at the nose of the spaceship, and move it in the correct direction.

In order to accomplish this, the first thing you need to do is create the Flash movieclip projectile/bullet and give it the linkage identifier name shot. The image below, zoomed to 200 percent, shows the mc-shot movieclip, in symbol editing mode, with the linkage identifier set to shot. The registration point is in the center of the graphic. For this graphic, the spaceship’s bullet is just a simple 5 pixel wide circle, created with the oval tool, and filled green with no stroke outline. To set the linkage identifier, right+click on the movieclip in the library, select properties and go to advanced.

Stage 2 Game Code (Stage 2 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;
   
    createEmptyMovieClip("shots_mc", 1);
    shipInit();
}

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–;
        }
    }
}

Video Tutorials

 In part 5, I create the projectile movieclip and the Actionscript for firing it.

In part 6, I write the Actionscript for moving the projectile in the correct direction

 

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 *