How to Make a Vertical Shooter in AS2 – Part 2

Part 2: Making it Shoot

Well, now that we’ve got the character moving, we have to make him able to shoot. The first step in doing this is to create a MovieClip which will be the bullet. Mine is 5×30 pixels. Give it a name of “mcBullet” with the capitals. We also are going to have to export it for actionscript. Hopefully by now, you know how to do this. But, I’ll explain it once again (for the 5th time).

Right click the MovieClip and click on “Linkage…”. A window will pop up. In this window, check off “Export for ActionScript” and leave everything just as it is.

Now, we can add actions to the bullet. Add the following code to the onEnterFrame() function:

	if(Key.isDown(32)){//if the space bar is pressed
		var bulletID:Number = Math.random(); //create a variable that we'll use at the bullet's id
		//then attach a bullet to the stage
		_root.attachMovie('mcBullet', 'Bullet'+bulletID,_root.getNextHighestDepth());
		//setting the coordinates of the bullet to be the same as the main character
		_root['Bullet'+bulletID]._x = mcMain._x + mcMain._width/2 - _root['Bullet'+bulletID]._width/2;
		_root['Bullet'+bulletID]._y = mcMain._y;
		_root['Bullet'+bulletID].onEnterFrame = function(){
			//giving the bullet some actions
			this._y -= 10; //moving the bullet
			if(this._y < -1 * this._height){//if the bullet goes off stage
				//then destroy it
				this.removeMovieClip();
			}
		}
	}

Now, when the user presses the space bar, a bullet should appear and fly upwards! It'll also disappear once it leaves the screen so it doesn't slow the game down. Now, the only problem with this script is that it doesn't limit the amount of time between each bullet shot. In order to do this, we must first add a few variables at the top of the code:

//BULLET TIMING VARIABLES
var cTime:Number = 0;//the amount of frames that has elapsed since last bullet shot
var cLimit:Number = 12;//amount of frames needed to shoot another bullet
var shootAllow:Boolean = false;//whether or not main can shoot

Next, add this code to the onEnterFrame function:

	cTime ++;//increment the time
	if(cTime == cLimit){//if enough time has elapsed
		shootAllow = true;//allow shooting again
		cTime = 0;//reset the time
	}

Now, the last two things we have to do is first make it so you can only shoot when shootAllow is set to true and set shootAllow to false when we shoot a bullet. First, simply change the if(Key.isDown[...]) statement for the space bar to if(Key.isDown(32) && shootAllow). Next, add shootAllow = false; to the end of that statement.

The final if statement should look a bit like this:

if(Key.isDown(32) && shootAllow){//if the space bar is pressed
		[...CODE...]
		shootAllow = false;
	}

Now if you test your movie, you should be able to shoot! Hurrah!

This concludes this part of the tutorial, stay tuned for the next one, creating enemies!

Preview

Download Source
(Requires Flash 8 or above)

Subscribe!

Subscribe!
Enter your email address:  

Awesome Tutorials