Tutorial: Make a Vertical Shooter in AS2


Written By MrSun at 8:01 am - Saturday, January 17th, 2009
Categories: Flash

Step 1: Programming the Character

Today, we’re going to make a classic vertical shooter game in ActionScript 2. I hope you learn from it! Let us begin.

The first thing that I’m going to do is make the background of my game black, so it looks more retro. Then, we’re going to have to make the frame rate faster, mine will be 24 fps. Also, this will be a vertical shooter, so we probably should make the playing screen more narrow. My new dimensions are at 300×400 pixels.

Next, we’re going to draw a character. Mine will be simple, just a triangle pointing upwards.
My character
The dimensions for it are 30×35 pixels.

Then, we’re going to turn it into a symbol. After that, we’re going to give it an instance name of mcMain for main character. We will need this so we can reference the guy later. Now we’re ready to code this sucker. Make a new layer called “actions” and add the following code:

var mainSpeed:Number = 5;//how fast the main guy can move

onEnterFrame = function(){ //this function will run every frame (needed for moving the character
	if(Key.isDown(37) || Key.isDown(65)){ //if the "A" key or Left Arrow Key is Down
		mcMain._x -= mainSpeed;//then the move the guy left
	}
	if (Key.isDown(38) || Key.isDown(87)){//if the "W" key or Up Arrow Key is Down
		mcMain._y -= mainSpeed; //then move the guy up
	}
	if(Key.isDown(39) || Key.isDown(68)){//if the "D" key or Right Arrow Key is Down
		mcMain._x += mainSpeed; //then move the guy to the right
	}
	if(Key.isDown(40) || Key.isDown(83)){//if the "S" key or Down Arrow Key is Down
		mcMain._y += mainSpeed; //then move the guy down
	}

	//keeping the main character within bounds
	if(mcMain._x <= 0){
		mcMain._x += mainSpeed;
	}
	if(mcMain._y <= 0){
		mcMain._y += mainSpeed;
	}
	if(mcMain._x >= Stage.width - mcMain._width){
		mcMain._x -= mainSpeed;
	}
	if(mcMain._y >= Stage.height - mcMain._height){
		mcMain._y -= mainSpeed;
	}
}

This is actually all we need for this chapter. Pretty easy, right? Next time, we’ll make him shoot!

Final Product

Source .fla File

14 Comments

Dieter:

Sorry for this stupid question but how to add a code


perry:

erm….. how do u get as2???


Kurren:

umm i had a problem with the coding when it is inserted into flash its code is completely benin.


Aoi:

@Dieter

First you make the thing you want to add a code to a symbol then press F9 so that the actions toolbar appears. ^_^

@perry

You dont have to get AS2, It is already built in from Flash 5 to Flash 8 i think.


Anon:

The Actionscript code is damaged, “guy” unresponsive to keystrokes


douchefag:

niggas niggas and niggas
your all niggas


matt:

press F9 to insert the codes


Thomas:

Im having a few problems i am currently using flash mx and the code doesnt seem to work i even copyed an d pasted it in and deleted all outside text there are 2 syntax errors i fix them as directed but my charecter doesnt move please help!


Thomas:

My charecter us not moving and i have 2 syntax errors im using flash mx here is the action script onClipEvent(load){
var mainSpeed:Number = 5;
}
onEnterFrame = function(){
if(Key.isDown(37) || Key.isDown(65)){
mcMain._x -= mainSpeed;
}
if (Key.isDown(38) || Key.isDown(87)){
mcMain._y -= mainSpeed;
}
if(Key.isDown(39) || Key.isDown(68)){
mcMain._x += mainSpeed;
}
if(Key.isDown(40) || Key.isDown(83)){
mcMain._y += mainSpeed;
}


Brian:

I have having some difficulty. “Statement block must be terminated by ‘}'”
I am using Adobe Flash CS4 and I am using Actionscript2, please help.


Greeny:

Worked Great for me, keep up the good tutorials


Anonymous:

What type of symbol do you convert the “character – triangle” into?


Harry:

Hey… i got a problem with keeping a guy within bounds…
What should i do?????


Joe Bob:

The code doesn’t work. Something is missing at the top.


«
»