Tutorial: Make a Vertical Shooter in AS3 – Part 2
Categories: Flash
Table of Contents
Step 2: Programming the Character Pt 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 “Bullet” 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 that we’ve got this down, we’re going to make a “Bullet” class. To do this, we have to make an external .as file. Press Command+N if you are on a Mac or Ctrl+N if you are on Windows and under “Type:”, select “ActionScript File”. Then, immediately save this file as “Bullet.as” in the same directory as your main game. Then, within it, type in the following code:
//This is the basic skeleton that all classes must have
package{
//we have to import certain display objects and events
import flash.display.MovieClip;
import flash.events.*;
//this just means that Bullet will act like a MovieClip
public class Bullet extends MovieClip{
//VARIABLES
//this will act as the root of the document
//so we can easily reference it within the class
private var _root:Object;
//how quickly the bullet will move
private var speed:int = 10;
//this function will run every time the Bullet is added
//to the stage
public function Bullet(){
//adding events to this class
//functions that will run only when the MC is added
addEventListener(Event.ADDED, beginClass);
//functions that will run on enter frame
addEventListener(Event.ENTER_FRAME, eFrame);
}
private function beginClass(event:Event):void{
_root = MovieClip(root);
}
private function eFrame(event:Event):void{
//moving the bullet up screen
y -= speed;
//making the bullet be removed if it goes off stage
if(this.y < -1 * this.height){
removeEventListener(Event.ENTER_FRAME, eFrame);
_root.removeChild(this);
}
}
}
}
This code really won't do anything by itself. We have to add it to stage whenever the player presses a button (the Space Bar). We also want to limit how quickly the user can shoot. This will be easy. Just add these variables to the top of the main code:
//how much time before allowed to shoot again
var cTime:int = 0;
//the time it has to reach in order to be allowed to shoot (in frames)
var cLimit:int = 12;
//whether or not the user is allowed to shoot
var shootAllow:Boolean = true;
Next, we have to add a timer that will increment the cTime variable every frame. We won't use the built in Timer class that ActionScript has (if you even know what that is) because going by frames is much more efficient. Add this code to the moveChar() function:
//Incrementing the cTime
//checking if cTime has reached the limit yet
if(cTime < cLimit){
cTime ++;
} else {
//if it has, then allow the user to shoot
shootAllow = true;
//and reset cTime
cTime = 0;
}
Now we can finally allow the shooting to occur. Add this code to the checkKeysDown() function.
//checking if the space bar is pressed and shooting is allowed
if(event.keyCode == 32 && shootAllow){
//making it so the user can't shoot for a bit
shootAllow = false;
//declaring a variable to be a new Bullet
var newBullet:Bullet = new Bullet();
//changing the bullet's coordinates
newBullet.x = mcMain.x + mcMain.width/2 - newBullet.width/2;
newBullet.y = mcMain.y;
//then we add the bullet to stage
addChild(newBullet);
}
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!
When I try to run this, it displays:
1190: Base class was not found or is not a compile-time constant.
… when referring to our package. What is wrong?
December 21st, 2008 at 3:42 pm
Even with your own code, my project complains about every variable of the Bullet.as not being defined; my bullet is named “Bullet” and everything is right on the Linkage properties.
Any help figuring out why it cannot locate these vars? (oh and yes, the .as file is in the same directory)
December 21st, 2008 at 3:53 pm
Every time i try to shoot. my character jumps to the right instead of shooting. There are no errors in my code. Please help
May 1st, 2009 at 1:28 am
the source FLA’s work, while taking this tutorial though i chose to swap out the simple shapes for some nicer ones, the problem i seem to get once the AS3 code is written or replaced, i get continual errors return when the ‘mcMain’ collides with ‘Enemy’
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/flash.display:DisplayObject::_hitTest()
at flash.display::DisplayObject/hitTestObject()
at Enemy/::eFrame()
Can anybody suggest a fix?
May 27th, 2009 at 1:13 am
Your tutorials are probably the most helpful game tutorials I have ever found, especially for developing with AS3 and FlashDevelop. I just wanted to mention a small detail that is missing from this step. When you do:
if(event.keyCode == 32 && shootAllow){
you should also set cTime = 0; This makes sure that the user really can’t fire two shots in less than 12 frames (try it and you’ll see!)
July 15th, 2009 at 4:01 pm
“1190: Base class was not found or is not a compile-time constant.” If you leave an instance of the movie clip on the stage this error comes up. It’s probably the one you initially created. Now that bullet is in the library you can delete it. Otherwise Flash has no clue how to deal with it. I’m a noob so I don’t know the technicalities behind it, but either way that’s how I solved it. Yey.
July 20th, 2009 at 5:58 am
made a little modification to my code 😉
makes it possible to continue shooting while holding space and moving at the same time 🙂
July 29th, 2009 at 7:03 pm
Shooting while moving diagonally doesn’t work except for right-up movement. Any idea why it works only in this direction?
I was trying to implement shooting while holding spacebar, however when I hold spacebar th mcMain stops moving diagonally. (again with exception for right-up direction)
August 26th, 2009 at 1:28 pm
does this work if the files are stored on a memory stick?
September 15th, 2009 at 8:52 am
If you want to fix the problem where you can’t shoot while moving and the fact that it sometimes shoots more than 1 at a time:
– don’t use the shootAllow variable. Instead created a spaceDown variable and create the bullet in the enter frame function
September 17th, 2009 at 9:12 pm
Magnus, could you please tell me what modification you made to your code to let it shout continiously?? =o
September 29th, 2009 at 11:25 am
THANK YOU!!!!!!!!!!!!!!!!!!!
it helps so much!!
January 3rd, 2010 at 11:12 am
@Magnus: How did you do it! I have the same issue!
zakkain at gmail.com – please get back to me!
March 10th, 2010 at 10:09 pm
I’m noticing that you can’t move diagonally up-left and shoot at the same time (in the demo here too). I’m having an analogous problem with something I’m working on- what’s up with that?
March 28th, 2010 at 6:50 pm
is it possible to set it up so that it shoots off of a mouse click instead? cause i have the function being run off of a MOUSE_DOWN mouse event and im haveing it place the bullet (it is exported thoguh action script) where the mouse clicks by saying newBullet.x = mouseX; and same with mouseY any thoughts as to why it wouldnt be working?
May 14th, 2010 at 1:20 pm