How to Make a Vertical Shooter in AS3 – 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 “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!

Spread the Knowledge

  • Print
  • Digg
  • del.icio.us
  • Facebook
  • StumbleUpon
  • Twitter
  • Google Bookmarks
  • Design Float
  • DZone
  • Yahoo! Buzz
  • PDF
  • Reddit
  • RSS
  • Technorati

Preview

Download Source
(Requires Flash CS3 or above)

Subscribe!

Subscribe!
Enter your email address:  

Awesome Tutorials