Adobe flash cs4 Game tutorial: shooting

Adobe flash cs4 Game tutorial: shooting

This tutorial will show you how to create a game with shooting functionality. This is a very simple thing to create. All the action script is in the timeline and the bullet.as file. We us the bullet class to control the bullet in the game.

Get Fla

bullet.as

TimeLine


19 comments:

Sam said...

enjoyed the tut - like seeing the a bit of oop incorporated

1 thing though - I thought data typing the constructor was a big no-no.

(in bullet.as, the constructor is data-typed to :void)

Guess this isn't that big of a deal, b/c everything works great.

Nathan said...

mine is not working, the bullets stay int he gorung pelase reply ASAP

Waarith Abdul-Majeed said...

I can't tell you what is wrong from your comment. I think that you didn't put value in for the speed of a bullet. ASAP? I guess your life depends on it.

Sean said...

Hey i'm making a game where its a guy who shoots sideways instead of a ship that moves up. and ideas on how to make the bullet fly right when he's turned right and left when he turns left? i can't figure it out.

Anonymous said...

Hi, I have a question to following line:
"parent.removeChild(this);" in the bullet.as

why does "bulletholder.removeChild(bl);" not work?

great tut, thank you :)

Unknown said...

Hi Waarith,
I am trying to create a fla where the spaceship can get navigated all over the stage and can shoot. But when I run the code, the bullets keep coming from the base of the stage instead of from the location of the ship. It appears that the position assigns in onclick() are not getting thru. What am i doing wrong? The entire timeline AS3 code is pasted below
============================
hero.gotoAndStop('still');

var sfx_gun:Sound = new sfx();

var Key:KeyObject = new KeyObject(stage);

var bulletholder:Sprite = new Sprite();

addChild(bulletholder);

addChild(hero);

stage.addEventListener(Event.ENTER_FRAME, game);
stage.addEventListener(MouseEvent.CLICK,onclick);


var sw:uint = stage.stageWidth;
var sh:uint = stage.stageHeight;

function game(e:Event):void{

if (hero.x > sw){
hero.x = 1;
}
if (hero.x < 1){
hero.x = sw-1;
}
if (hero.y > sh){
hero.y = 1;
}
if (hero.y < 1){
hero.y = sh-1;
}

if (Key.isDown(Key.UP)){
var angle_in_radians = Math.PI*hero.rotation/180;
if (hero.rotation > 90 && hero.rotation < 180)
{
hero.x -= 5*Math.tan(angle_in_radians);
hero.y += 5;
}
else if (hero.rotation > 270 && hero.rotation < 360)
{
hero.x += 5*Math.tan(angle_in_radians);
hero.y += 5;
}
else
{
hero.x += 5*Math.tan(angle_in_radians);
hero.y -= 5;
}
hero.gotoAndStop('movin');
}
/* else {
hero.gotoAndStop('still');
}
*/
/* else if (Key.isDown(Key.DOWN)){
hero.y += 5;
hero.gotoAndStop('movin');
} */ // this movement is not needed
else if (Key.isDown(Key.RIGHT)){
hero.rotation += 2.5;
hero.gotoAndStop('moving');
}
else if (Key.isDown(Key.LEFT)){
hero.rotation -= 2.5;
hero.gotoAndStop('moving');
}
else {
hero.gotoAndStop('still');
}

} // end game

function onclick(e:Event) {
sfx_gun.play();
var bl:Sprite = new bullet();
bl.y=hero.y;
bl.x=hero.x;
bulletholder.addChild(bl);
}

Anonymous said...

Hey man! This is so awesome, thanks a lot! But could you do another detailed shooter tutorial, for a run and gun Metal Slug style game? I have a platform game I'm making, and I want to know how to make him shoot whilst he's running, and whilst he's jumping. Would this be possible?

Anonymous said...

Amazing Tutorial! I was wondering if you could make one where the ship can be effected when the enemy touches it.

And maybe one day you can make one for first person shooter games too! :)

Anonymous said...

i get dis error at run time

1046: Type was not found or was not a compile-time constant: bullet.

wat shd i do ?

Anonymous said...

your websites tutorials are horrible. none of its works. the output said that everysingle line is wrong and to remove all of it.

Anonymous said...

hey, if anybody got d perfect working code of the shooting pls post it.! the given code didnt work out for me :(

thanks in advance

Arman said...

is there a way to make the bullets shoot in the other direction? I made it so the bullets shoot horizontally, but when I turn they won't go the other way. Please help me, my programming grade depends on it!

Hero said...

Great tutorial, but it dosen't work. I used the "hero" from one of your previous tutorials, equipped with jumping fuctionalities. However, when i used the code, they gave me an error :
Location|Description
Scene 1 |1046: Type was not found or was not a compile-time constant: hero.
how to fix?

Trenton said...

All the people saying your tuts are bad or this script doesnt work. your wrong! the script works fine i did everything he did and i have a perfect game. dont critisize him obviously you made a mistake and apparently you need HIS help. because you cant fix it on your own. in other words...
GREAT TUTORIAL!!!!! can u email me at ssmstudios@live.com and tell me how to change it to keys and space bar thanks

ToyKitteh said...
This comment has been removed by the author.
Fangrider said...

I downloaded the FLA and i think its Flash CS5 thats the problem because your code just doesant seem to work it always makes the bullets get stuck at the bottom and not move one bit

Anonymous said...

does this only work in cs4? can it work in cs3? >.<

Anonymous said...

i cannot get mine to work. i have all of the exports right and everything. i have the bullet.as and the game file .fla but when i run the game there is nothing there. ocasionally the sfx plays when it first starts up but that is it. thanks

Anonymous said...

Great Tutorial! This code helped me out a lot as a guideline. But one thing I could really use is how to add a score when enemies die. I've tried what seemed like everything but cannot get it to display correctly... So if you could do some type of tutorial that focuses on Scoring and Lives that would be great THANKS!!!!