Tutorial: Make a Vertical Shooter in AS2 – Part 2
Categories: Flash
Table of Contents
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!
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?
January 27th, 2009 at 5:10 pm
This part confuses me, where and in what order does the code go?
March 24th, 2009 at 6:50 pm
what confuses me is where to put the bullet timing variables
April 9th, 2009 at 11:05 pm
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)
April 16th, 2009 at 3:01 pm
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.
April 30th, 2009 at 1:48 pm
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!
May 9th, 2009 at 7:00 am
where should I put the first code?
May 12th, 2009 at 3:43 am
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…
May 12th, 2009 at 5:21 am
Hi, how do you increase the speed of the bullet?
June 22nd, 2009 at 5:37 pm
@LeTapir just increase the numer 10 in (this._y -= 10; //moving the bullet)
July 3rd, 2009 at 3:57 pm
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.
October 30th, 2009 at 3:57 pm
What exactly is _root[]? I’d Google it but Google ignores braces.
November 21st, 2009 at 5:32 pm
who ever made this tut is a dumb ass they need to be more detailed! where do i put the codes?!?!?!?!?!
November 22nd, 2009 at 11:23 pm
I dont have the linkage thingy what should i do!?!?!?!
November 27th, 2009 at 8:24 pm
mine works, but it will only shoot one bullet, when i press space again, it deletes the previous bullet and shoots a new one…
January 6th, 2010 at 9:32 am
Use select tool and highlight the bullet. Then press F9 and do the ….ing tutorial
January 30th, 2010 at 9:37 pm
It isn’t working, either I do not know what I am doing, or the coding is all wrong
February 25th, 2010 at 10:54 pm
hey, where i must put the last code?
March 4th, 2010 at 8:18 am
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
};
April 17th, 2010 at 11:40 am
I am confused on where to put the onEnterFrame function:codes…. can someone tell me?
May 12th, 2010 at 11:15 am
When you click ‘Export to Actionscript’ when making the bullet, set the identifier name as mcBullet. that is
how it got it to work.
May 29th, 2010 at 8:30 am