Best viewed in Firefox

Awesty Productions

Simple Shooting System

December 28th, 2006 by awesty

In this tutorial you will be learning how to make a simple shooting system that pauses between shots so it doesn’t fire rapidly.

Here is an example: Link

First of all, you need to make the thing that will be shooting. Make sure it is birdseye/top view. Make this an MC (movieclip).

Give this MC an instance name of “man”. Now double click that MC and make a new layer underneath the layer that your “man” is on. On this layer draw your bullet and make that an MC as well. Give this an instance name of “bullet”. Now put this code on it:

onClipEvent(enterFrame){
    if(this._name != "bullet"){
        this._y -= 5;
    }
}

Line 1 basically means do the following code every frame. Line 2 is making sure that the MC isn’t called “bullet”, before doing the code between the curly braces. This is so when we duplicate the MC, it has a different name. So this code makes sure only the duplicated MC’s move, not the original because then there is nothing to duplicate.
The 3rd line is making the MC’s _y coordinates decrease by 5 (moving upwards).

Now you need to place this code on the “man” MC:

onClipEvent(load){
    var i:Number = 0;
    var load:Number = 10;
}
onClipEvent(enterFrame){
    if(load > 0){
        load–;
    }else{
        if(Key.isDown(Key.SPACE)){         
            i++;                                                duplicateMovieClip(_root.man.bullet,"bullet"+i,i);
            load = 10;
        }
    }
}

Lines 1-4: This is going to happen when the MC loads, and only then. It is making two new variables, i and load, which are both numbers.
Lines 6-8: Line 6 is an if statement, checking if load is greater than 0. If it is, it is going to run line 7, which is decreasing load by 1. If the if statement is false, it is going to run the code between the “else{}” curly braces.
Lines 9-13: Line 9 is an if statement checking if the space bar is down. If it is, it runs the following code. i++ is increasing i by 1, so then all the duplicated MC’s have different names and depths for next time.
duplicateMovieClip is a function that has 3 parameters. Target, name and depth. Target is the MC you are going to be duplicating, name is the new name for the duplicated MC and depth is the depth of the new MC. All MC’s need a different depth.
You can see that the target is _root.man.bullet, which is the bullet. But since it is inside “man” you need to have man there to.
The name is “bullet”+i. i is a variable, so if i is equal to 3, then its name would be bullet3. Since i increases every time an MC is duplicated, none of then have the same name. Same with depth.

Now you have a fully functioning shooting system.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • del.icio.us
  • digg
  • Furl
  • MyShare
  • NewsVine
  • Netscape
  • Reddit
  • Simpy
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

RSS feed

126 Comments »

Comment by Bulletluigi
2006-12-28 12:18:50

Wow, really nice tutorial. If you do Shooting System II, you should tell how to make the bullets fire at the same angle as the turret. I have been trying to find a good tutorial on that for a while, so I can finally do this tank game that my friend has been bugging me about.

 
Comment by Jake
2006-12-28 13:00:58

Thank you so much, you ROCK!!! You’re a prodigy for your age, lol

 
Comment by Bob
2006-12-28 13:20:39

Man…I wish I were half as good as you. This tutorial is awesome. keep ‘em comming.

 
Comment by Jake
2006-12-28 17:25:15

haha… I just realized how pointles that was, you walk, and the ground scrolls, so all I accomplished was making you look like your going faster than you really are… (you can delete that comment)

 
Comment by awesty
2006-12-28 17:34:55

Thanks guys ;)

 
Comment by Projesh
2006-12-29 03:16:05

SUPERB !

KEEP SOME MORE COMING IN !

 
Comment by awesty
2006-12-29 10:49:30

btw guys, line 7 on the second block of code should have an extra ‘-’. It kept getting taken out.

 
Comment by Jake
2006-12-30 07:36:02

Yeah, I got an error message and my brane said, if plus one is then minus one is –, lol

 
Comment by Jake
2006-12-30 07:41:56

…..it does take the second minus out…

 
Comment by eblup
2006-12-30 10:31:29

hey thanks but how do you pit links to youre flash games.

 
Comment by Oakheart
2006-12-30 12:00:01

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 7: Syntax error.
load–;

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 8: ‘else’ encountered without matching ‘if’
}else{

Total ActionScript Errors: 2 Reported Errors: 2

help pweas

 
Comment by Bryce K :P
2006-12-30 14:04:27

i do not freakin get this, do i make my man and my bullet on the same frame?

 
Comment by Jesse
2006-12-30 17:43:43

Thanks for all the tutorials! I had purchased flash in college when it was cheap (educational price) but had never gotten around to learning it.

Unless flash does this automatically (and I would guess it wouldn’t as it would be weird if it did), you should “garbage collect” the bullets once they are not useful.

I did this by added:

if (this._y < 0) removeMovieClip(this);

inside the bullet’s onClipEvent.

 
Comment by awesty
2006-12-31 13:14:45

I REPEAT:

btw guys, line 7 on the second block of code should have an extra ‘-’. It kept getting taken out.

Please make sure you have a quick read through the comments so I don’t have to answer the same questions 50 times.

@Bryce K: Yes.

@Jesse: Yea, I was going to add that but forgot/got lazy.

 
Comment by Oakheart
2006-12-31 14:22:26

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 7: Syntax error.
load–-;

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 8: ‘else’ encountered without matching ‘if’
}else{

Total ActionScript Errors: 2 Reported Errors: 2
ok i did that and re-did everything a few times, in different ways, still not working!

 
Comment by awesty
2006-12-31 14:52:33

Can you please post the whole code you are using?

 
Comment by Oakheart
2006-12-31 15:03:27

onClipEvent(load){
var i:Number = 0;
var load:Number = 10;
}
onClipEvent(enterFrame){
if(load > 0){
load–-;
}else{
if(Key.isDown(Key.SPACE)){
i ; duplicateMovieClip(_root.man.bullet,”bullet” i,i);
load = 10;
}
}

 
Comment by RDB2006
2007-01-01 01:03:30

I added the extra - on line 7 but these 3 errors keep appearing…

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 7: Syntax error.
load–-;

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 8: ‘else’ encountered without matching ‘if’
}else{

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 14: Unexpected ‘}’ encountered
}

Total ActionScript Errors: 3 Reported Errors: 3

 
Comment by awesty
2007-01-01 11:57:25

Which version of flash are you using?

You can send the .fla to:

awestyproductions[@]hotmail[.]com

 
Comment by RDB2006
2007-01-01 20:54:53

kk thanks. Im using Flash MX 2004 educational.

 
Comment by Bobbkins
2007-01-02 03:05:21

hi i have a question. Could you explain how you can press a button, and then it takes you to an MC’s timeline on a certain frame, not the normal timeline.
Thanks awesty

 
Comment by awesty
2007-01-02 10:29:42

if(Key.isDown(Key.LEFT)){
_root.mc.gotoAndStop(FRAME);
}

 
Comment by Salizer
2007-01-03 00:08:16

I also get this errors and I’m using Flash 8 Pro:

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 7: Syntax error.
load–-;

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 8: ‘else’ encountered without matching ‘if’
}else{

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 14: Unexpected ‘}’ encountered
}

Total ActionScript Errors: 3 Reported Errors: 3

 
Comment by Salizer
2007-01-03 00:12:14

I got it alright now.

Just wrote loading instead of load, and putted and extra ‘}’ in.

onClipEvent(load){
var i:Number = 0;
var loading:Number = 10;
}
onClipEvent(enterFrame){
if(loading > 0){
loading -=1;
}else{
if(Key.isDown(Key.SPACE)){
i ; duplicateMovieClip(_root.man.bullet,”bullet” i,i);
loading = 10;
}
}
}

 
Comment by awesty
2007-01-03 11:33:21

Yea, load is already a property… or a method I can’t think right now, so flash would have gotten confused.

 
Comment by RDB2006
2007-01-04 00:56:36

hi agen. I’m having yet another problem. For a game i’m making ‘m putting this code on my mc, but loads of errors keep coming up, and when i try and fix them more pop up and it just isn’t working. Could you please have a look and if you can change it plz post the correct code.

onClipEvent(enterFrame){
if(Key.isDown(Key.RIGHT)){
this._x = 6.5;
_root.fuel -=0.1
}
}
onClipEvent(enterFrame){
if(Key.isDown(Key.LEFT)){
this._x -= 6.5;
_root.fuel -=0.1

onClipEvent(enterFrame){
if(Key.isDown(Key.UP)){
this._y -= 6.5;
_root.fuel -=0.1
}
}
onClipEvent(enterFrame){
if(Key.isDown(Key.DOWN)){
this._y = 6.5;
_root.fuel -=0.1
}
}
onClipEvent(enterFrame) {
if(Key.isDown(72)){
_root.info = (”Use the arrow keys to move. Avoid the enemy’s spikes and try and destroy him! You will get 50 coins every time you win.”)
_root.count = 5;

 
Comment by awesty
2007-01-04 08:38:24

Is root info dynamic text? If so try this:

onClipEvent(enterFrame){
if(Key.isDown(Key.RIGHT)){
this._x += 6.5;
_root.fuel -=0.1
}
if(Key.isDown(Key.LEFT)){
this._x -= 6.5;
_root.fuel -=0.1
}
if(Key.isDown(Key.UP)){
this._y -= 6.5;
_root.fuel -=0.1
}
if(Key.isDown(Key.DOWN)){
this._y += 6.5;
_root.fuel -=0.1
}

if(Key.isDown(72)){
_root.info = ”Use the arrow keys to move. Avoid the enemy’s spikes and try and destroy him! You will get 50 coins every time you win.”
_root.count = 5;
}
}

 
Comment by eblup
2007-01-05 12:33:10

my birthday is January 8th, 2007

 
Comment by wikiwiki
2007-01-18 11:42:06

yea im gettin this and im usin flash pro 8

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 7: Syntax error.
load–;

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 8: ‘else’ encountered without matching ‘if’
}else{

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 14: Unexpected ‘}’ encountered
}

Total ActionScript Errors: 3 Reported Errors: 3

 
Comment by wikiwiki
2007-01-18 11:48:28

k i changed it to salizers n this happened :S :S :S

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 11: This type of quotation mark is not allowed in ActionScript. Please change it to a standard (straight) double quote.
i ; duplicateMovieClip(_root.man.bullet,”bullet” i,i);

Total ActionScript Errors: 1 Reported Errors: 1

 
Comment by drake
2007-01-19 02:43:12

hey awesty your tutorials are good, i cannot w8 for the next one! I’m creating an RPG and ran into a slight problem. I was wondering if you could help me with it.
My man starts off at the centre of the screen. You can walk into a house and it takes you to the interior. When you come out the house it puts my man back to the centre. Can you please send me some code that will make the m an appear outside beside the door? thanks

 
Comment by awesty
2007-01-19 14:45:33

@wikiwiki: Just remove the qoutation marks, then put them back in. Don’t copy and paste them though.

@drake: Well when you exit the house, make a variable like exitHouse true. And on that frame outside the house put
if(exitHouse == true){
_root.MC._x = NUMBER;
_root.MC._y = NUMBER;
}

Just change MC to the instance name of the MC, and NUMBER to the _x and _y coordinates you want him to be when he exits the house.

 
Comment by drake
2007-01-19 18:11:52

ok i put this code on the frame (The outside of the house)

stop()
if(exitHouse == true){
_root.man3._x = 264;
_root.man3._y = 110;
}

and then on the door to the inside of the house i put this

onClipEvent(enterFrame){
if(_root.man3.hitTest(this)){
gotoAndStop(2)
}
}

and on the door in the interior, i put this

onClipEvent(enterFrame){
if(_root.man3.hitTest(this)) {
exitHouse == true
gotoAndStop(1);
}
}

but nothing happens. Please tell me if iv’e done something wrong. thanks.

 
Comment by drake
2007-01-19 18:15:40

i fixed half the problem, i added
_root.
before gotoAndStop. But when i exit the interiror t takes me back to frame 1 but not at the coords i asked it for

 
Comment by Mattsta
2007-01-20 23:14:52

yea i keep getting the same error as well can some 1 plaz post some help?

 
Comment by awesty
2007-01-21 20:13:55

When you declare the exitHouse variable, you need to put _root. infront of it (_root.exitHouse), aswell as when you refer to it.

 
Comment by Bryce K.
2007-01-23 06:26:52

can i see someones flash?? i wanna play it :P

 
Comment by tim
2007-01-25 12:10:49

i have the same problem as drake, only i have a bunch of buildings. is there a simpler script to change the position of the man when he exits a building, or do i have to write multiple scripts for each building and each time the man exits a building?

 
Comment by awesty
2007-01-26 12:03:18

The easiest way would be the second way. But I am sure there would be a shorter and less time consuming way.

 
Comment by big al
2007-01-26 13:52:34

HELP!!!

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 11: ‘)’ or ‘,’ expected
duplicateMovieClip(_root.man.bullet,”bullet” i,i);

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 15: Unexpected ‘}’ encountered
}

Total ActionScript Errors: 2 Reported Errors: 2

 
Comment by big al
2007-01-26 13:54:32

almost forgot…here is the code i have on man:

onClipEvent(load){
var i:Number = 0;
var loading:Number = 10;
}
onClipEvent(enterFrame){
if(loading > 0){
loading -=1;
}else{
if(Key.isDown(Key.SPACE)){
i ;
duplicateMovieClip(_root.man.bullet,”bullet” i,i);
loading = 10;
}
}
}

 
Comment by awesty
2007-01-26 16:26:01

Just try retyping all the quotation marks.

 
Comment by big al
2007-01-27 11:19:13

thanks

 
Comment by big al
2007-01-27 11:31:08

im hopeless…now it dont shoot…

 
Comment by awesty
2007-01-27 12:08:57

lol. Did you change the code at all?

 
Comment by big al
2007-01-28 14:10:23

no… just retyped the quotation marks, and deleted the #s that pop up before each line when i copy and paste

 
Comment by big al
2007-01-28 15:01:25

hahaha…got it…i had to mixs some things up… but for some reason, if i hold the space key down…. the bullet just stay over the man… and if i press it again, while the old bullet hasnt finished, it deletes it and starts over

 
Comment by awesty
2007-01-28 19:11:42

Weird.

 
Comment by big al
2007-01-29 03:18:31

wtf???

 
Comment by big al
2007-01-29 08:05:21

awesty, can i send u the flash or somethin, cause this aint workin….

 
Comment by awesty
2007-01-29 08:40:28

I dont have the .fla. My computer crashed. >_<

 
Comment by big al
2007-01-29 12:11:35

is there a way i can get a guy to jump…but if i also press RIGHT, or LEFT, he will jump in that direction? i have all the simple controls down… back, forward, attack, stand, duck, jump…

 
Comment by Grifo
2007-01-29 12:39:33

Sure there is, but the way to do it depends on how you coded everything till now. I’m no good at code part and my codes are always a mess, but if you have the movement coordinates in the guy’s MC(instead of the walkin MC) you should be able to move him even while jumping. I can’t make it clearer this way, so, if you have some knowledge in flash, you should understand that. all you need is some variables to set to true when running, the same with jumping.

 
Comment by Bryce K
2007-01-29 13:03:59

Man i do not get this tutorial and i no everything else :P

 
Comment by awesty
2007-01-29 20:33:39

@Bid al: Ah… well in the jumping code, just basically add the walking code, but without going to the walking frame.

 
Comment by big al
2007-01-30 06:57:35

ya, but is there a way i can make him jump in both direction…like is he is facing right, he goes right, if he is facing left, he goes left

 
Comment by big al
2007-01-30 07:44:55

and ya, can u make a tutorial, where you teach how to make the hands of a “man” turn after the mouse…. like in heli attack

 
Comment by awesty
2007-01-30 09:36:35

@big al: If you have the walking code, and the walking code does that you can just add it into the jumping code.

About the tutorial, yea i will try.

 
Comment by Bryce K
2007-01-30 12:06:04

can i get the .fla for this TUT cause i do not get this at all, if you could send it to robinweaver@san.rr.com labled as SHOOTING GAME FLA

 
Comment by big al
2007-01-30 12:47:54

thx… il try

 
Comment by big al
2007-01-30 13:40:08

you can call me a complete noob if you like, but im not sure i get it… here is what i have on my “man” mc… can you show me the change

onClipEvent(load){
fight = false;
}
onClipEvent(load){
fight = false;
}
onClipEvent(enterFrame){
if(fight == false){
if(Key.isDown(Key.LEFT) && fight != true){
this._x -= 5;
this._xscale = -100;
this.gotoAndStop(2);
}else if(Key.isDown(Key.RIGHT) && fight != true){
this._x = 5;
this._xscale = 100;
this.gotoAndStop(2);
}else{
this.gotoAndStop(1);
}
}
if(Key.isDown(Key.SPACE)){
this.gotoAndStop(3);
fight = true;

}
if(Key.isDown(Key.UP)){
this.gotoAndStop(4);
fight = true;

}
if(Key.isDown(Key.DOWN)){
this.gotoAndStop(5);
fight = true;
}
if(this._currentframe == 1){
fight = false;
}
}

 
Comment by awesty
2007-01-30 18:58:29

ah… there isnt a jumping code in there :\

 
Comment by big al
2007-01-31 02:00:58

ya there is… the key.UP line..thats wat i have as jump

 
Comment by awesty
2007-01-31 15:44:37

Oh, so the jumping isnt actually coded, it is just a jumping animation on the fourth frame of the MC?

 
Comment by help plez
2007-02-01 08:14:54

i put in this code onClipEvent(load){
var i:Number = 0;
var load:Number = 10;
}
onClipEvent(enterFrame){
if(load> 0){
-load–;
}else{
if(Key.isDown(Key.SPACE)){
i++; duplicateMovieClip(_root.man.bullet,”bullet”+i,i);
load= 10;
}
}
}
nothing works and it says there is no errors so can u write the complete correct cod in a comment thanx really good tutorials but i always get lost in the last parts like the complete fighting game one but other then that excilent tutorials

 
Comment by big al
2007-02-01 08:39:48

ya, is there something i can do here?

 
Comment by Bryce K
2007-02-01 13:53:34

ummm big al try this code,

http://www.swfup.com/swf-view.php?id=5239

go to this website get the code and shad up
read awesty’s jumping tutorial to understand it

 
Comment by awesty
2007-02-01 16:15:46

@help: You put the extra hyphen directly next to the one already there.

 
Comment by big al
2007-02-02 04:41:13

bryce, 2 probs, first the site u gave me doesnt let me copy and paste the code… 2) i did use awesties tutorial, and i ended up having what i have, i just upgraded his “moving with arrow keys”

 
Comment by Bryce K
2007-02-02 11:51:14

dude your supposed to write it from scratch!!! nah im just joking, hmm… sorry hold on ill do it again,

 
Comment by Bryce K
 
Comment by big al
2007-02-03 05:10:03

this should go onto the man movie clip, right?

 
Comment by Bryce K
2007-02-03 09:39:47

yup insted of your UP Key use this also change the numbers how you want ‘em (use awesty tutorial to do that)

 
Comment by big al
2007-02-05 11:03:31

thanks dude.

 
Comment by SilentThunder775
2007-02-22 16:46:04

I put in the following code, but for some reason i is always equal to zero or something, because the old bullet previously shot always dissappears when I shoot the new bullet(I changed a few things to make it work :)) Anyway here’s the code:
onClipEvent(load){
i = 0;
}
onClipEvent(enterFrame){
i++;
if(Key.isDown(Key.SPACE)){
this.duplicateMovieClip(_root.man.bullet_mc,”bullet” + i,i);
}
}
//I didn’t change any code in the bullet movie //clip actions, so they’re the same as the ones in //your tutorial.

 
Comment by SilentThunder775
2007-02-22 16:49:12

I didn’t care for the timer thing, so I took it out.

 
Comment by problem
2007-02-24 14:20:06

So there are no errors, but the bullet doesn’t fire. In my case, I named my ‘bullet’ as ‘fireball’, and changed from ‘load’ to ‘reload’. Can you see what the problem might be?

onClipEvent (load) {
var i:Number = 0;
var reload:Number = 10;
}
onClipEvent (enterFrame) {
if (reload>0) {
reload–;
} else {
if (Key.isDown(Key.SPACE)) {
i++;
duplicateMovieClip(_root.man.fireball, “fireball”+i, i);
reload = 10;
}
}
}

 
Comment by problem
2007-02-24 14:30:42

Oh yea, the reload DOES actually have 2 minuses, but for some reason its not showing up here.

 
Comment by awesty
2007-02-25 13:00:58

problem: which version of flash are you using?

 
Comment by problem
2007-02-25 23:38:13

I’m using flash 8, so it shouldn’t be a problem as far as I can tell.

 
Comment by jacob
2007-03-01 18:15:43

hi i have a problem. It works fine but ive tried adding this code to an enemy:

onClipEvent (enterFrame) {
if (_root.bullet.hitTest (this)) {
_root.bullet.gotoAndStop(”invisible”)
_root.bullet -= 1
_root.bosshp -=25;
}
}

onClipEvent(enterFrame){
if(this.hitTest(_root.man)){
_root.hp -=6;
}
}

i want it so that when you press space and the bullet hits the enemy, life is taken off and the bullet goes to a frame in its own timeline (inivisble) and disapears. It will take off 6hp of me when i hit it with my man but nothing happens with the bullet. can you help?

 
Comment by Problem
2007-03-04 08:10:54

Jacob, remember in the code, each bullet is bullet+i

you are targetting the main bullet with that, and thus nothing will happen. You would have to incorporate the loop into that code I think.

Either that or put the hittest code onto the bullet.

 
Comment by awesty
2007-03-05 16:47:35

@Problem: What are the errors you are getting?

@Jacob: Yes, listen to Problem.

 
Comment by Problem
2007-03-09 15:15:51

I’m not getting any errors, its just not shooting.

 
Comment by big al
2007-03-10 11:18:49

hey, long time no see…anyway i had a q…i found out recently that u use wordpress…so do i…but howd u place the swf files for the animated tutorails u have?

 
Comment by awesty
2007-04-05 13:34:38

@big al: I have a plugin. Its called Kimili Flash Embed.

 
Comment by blah blah Subscribed to comments via email
2007-04-22 21:30:12

heheehehehe i will kill you hahaha

 
Comment by bob Subscribed to comments via email
2007-04-23 10:25:34

what about side view??

Comment by awesty
2007-04-23 17:35:31

Well if you rotated everything to its right or left would be side view wouldnt it ;)

 
 
Comment by Johnpinkertin Subscribed to comments via email
2007-04-29 07:12:05

ok im sorry i read all the coments and i tried putting the secound - in line 7 but i still keep getting these errors PLZ HELP ME

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 7: Syntax error.
load–;

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 8: ‘else’ encountered without matching ‘if’
}else{

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 14: Unexpected ‘}’ encountered
}

Total ActionScript Errors: 3 Reported Errors: 3

Comment by awesty
2007-04-30 16:33:44

Did you change the code in any way? I would try retyping the code instead of copying and pasting it. Sometimes when you paste code into flash some characters get turned into other characters.