Tutorial: Make a Vertical Shooter in AS2 – Part 2


Written By MrSun at 8:02 am - Saturday, January 17th, 2009
Categories: All Tutorials, AS2, Flash, Game Development, Intermediate Tutorials

Step 2: Programming the Character 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!

Final Product

Source .fla File

21 Comments

Spitzer:

I only get the bullet to appear in the upper left corner after inserting the first code part in Part 2, what am I doing wrong?


Ben:

This part confuses me, where and in what order does the code go?


martin:

what confuses me is where to put the bullet timing variables


Tagori:

Spitzer, i had that problem too, it turns out I put bullet in for ship, just mess with some things a little (unless you’re making an exact clone of this game)


J:

Where do I put the code for the bullet? In the same place as the mcMain’s code? Because I got that part to work, but not the rest unfortunately.


Hamid:

The Code for the bullet should go within the onEnterFrame of the mcMain. Stick the code at the end of the page boundry code but before the “}” bracket. All the 3 new vars should be put after the “var mainspeed:number =5″ is. If you got more Qs download the source code and it will answer most of your questions about where the codes should go. For more Qs email me on hamid.afshar@hotmail.co.uk.
Thanks Mr Sun,
It is a good tutorial. Nice one!


bowatatoy:

where should I put the first code?


Elmo:

You have to be more spesific… Write down stepp by step ! how to do this and write down every thing you klick on. try using demobuilder…


LeTapir:

Hi, how do you increase the speed of the bullet?


WJ:

@LeTapir just increase the numer 10 in (this._y -= 10; //moving the bullet)


Chris:

i dont uderstand where to put the

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

i tried it and nothing shoots :S and i cant even download it…

sorry but these tutorials are not that easy to read.


Jack:

What exactly is _root[]? I’d Google it but Google ignores braces.


bryan451:

who ever made this tut is a dumb ass they need to be more detailed! where do i put the codes?!?!?!?!?!


Thomas:

I dont have the linkage thingy what should i do!?!?!?!


Anonymous:

mine works, but it will only shoot one bullet, when i press space again, it deletes the previous bullet and shoots a new one…


MarcusD:

Use select tool and highlight the bullet. Then press F9 and do the ….ing tutorial


Greeny:

It isn’t working, either I do not know what I am doing, or the coding is all wrong


Anonymous:

hey, where i must put the last code?


Helper:

I see that alot of you are having problems with the code. Heres how it should look:

var mainSpeed = 5;
var cTime = 0;
var cLimit = 12;
var shootAllow = false;
onEnterFrame = function ()
{
if (Key.isDown(37) || Key.isDown(65))
{
mcMain._x = mcMain._x – mainSpeed;
} // end if
if (Key.isDown(38) || Key.isDown(87))
{
mcMain._y = mcMain._y – mainSpeed;
} // end if
if (Key.isDown(39) || Key.isDown(68))
{
mcMain._x = mcMain._x + mainSpeed;
} // end if
if (Key.isDown(40) || Key.isDown(83))
{
mcMain._y = mcMain._y + mainSpeed;
} // end if
if (Key.isDown(32) && shootAllow)
{
var _loc3 = Math.random();
_root.attachMovie(“mcBullet”, “Bullet” + _loc3, _root.getNextHighestDepth());
_root["Bullet" + _loc3]._x = mcMain._x + mcMain._width / 2 – _root["Bullet" + _loc3]._width / 2;
_root["Bullet" + _loc3]._y = mcMain._y;
_root["Bullet" + _loc3].onEnterFrame = function ()
{
this._y = this._y – 10;
if (this._y < -1 * this._height)
{
this.removeMovieClip();
} // end if
};
shootAllow = false;
} // end if
++cTime;
if (cTime == cLimit)
{
shootAllow = true;
cTime = 0;
} // end if
};


TP:

I am confused on where to put the onEnterFrame function:codes…. can someone tell me?


Will:

When you click ‘Export to Actionscript’ when making the bullet, set the identifier name as mcBullet. that is
how it got it to work.


«
»