Making a Fight Game
In this tutorial you will learn how to make a fight game in flash. I will only be teaching you how to set up the character and the actionscript, not how to animate the character.
Click Here to see the final product.
This tutorial will be a whole lot easier if you have already taken my Moving MovieClips with the arrow keys, Making walls with hitTests and Making a health system tutorials since alot of the script used in those will be used in this tutorial so I won’t go into as much detail with them as I might with the rest of the script.
First of all, you need to make your character. You need to make a movieclip of him standing still, walking/runnning and punching/kicking/attacking.
When you have got all of these I want you to put them all into one single movieclip. With the standing movieclip on the first frame, the walking/running on the second and the attacking on the third. Give this MovieClip an instance name of ‘man’. It is case sensitive
Select the first frame of this newly created movieclip and put this action on it:
That just means ‘Stop on this frame’. Repeat this with the other two frames. That is so the movieclip doesn’t loop.
Now return to the main timeline and click on you ‘man’ movieclip and put these actions on it.
fight = false;
}
onClipEvent(enterFrame){
if(fight == false){
if(Key.isDown(Key.LEFT) && fight != true){
this._x -= 4;
this._xscale = -100;
this.gotoAndStop(2);
}else if(Key.isDown(Key.RIGHT) && fight != true){
this._x += 4;
this._xscale = 100;
this.gotoAndStop(2);
}else{
this.gotoAndStop(1);
}
}
if(Key.isDown(Key.SPACE)){
this.gotoAndStop(3);
fight = true;
}
if(this._currentframe == 1){
fight = false;
}
}
That is the main peice of code you need for this tutorial, but you are not done yet. If you test your movie now (Ctrl+Enter) there will still be some bugs which we will fix after I finish explaining the code you just got.
fight = false;
}
That means when this movieclip loads, the variable fight is equal to false. This variable is used to see whether the ‘man’ movieclip is attacking or not, so he doesn’t walk and attack at the same time. If you make him jump this is handy also because otherwise if you attack in mid-air he will freeze and stop jumping.
if(fight == false){
</blockquote>
<p>Every frame, if the variable fight is equal to false…
</p>
<blockquote>if(Key.isDown(Key.LEFT) && fight != true){
this._x -= 4;
this._xscale = -100;
this.gotoAndStop(2);
I the left arrow key is down and (&&) fight is NOT equal to (! is the symbol for not in most coding languages, so != means not equal to) true, this _x coordinate decreases by 4 pixels (therefor it moves left), this _xscale equals -100 (an _xscale of 100 is normal width, one of 50 is half its normal width, one of -100 is its normal width but facing the other way, so then ‘man’ faces left when he walks left), this go to and stop on frame number 2 which should be the walking frame.
this._x += 4;
this._xscale = 100;
this.gotoAndStop(2);
The above code (it is the same as this but for left instead of right) won’t happen unless this code is happening. The rest of the code you should recognise from before, some numbers are just changed around and LEFT is RIGHT.
this.gotoAndStop(1);
}
If the two above code aren’t running, this can go to and stop on frame number 1, the idle/standing still frame.
this.gotoAndStop(3);
fight = true;
}
If the space bar in down, this can go to and stop on frame number 3 (the attacking frame) and fight is equal to true.
fight = false;
}
If this movieclips is currently on frame one (the idle frame), fight equals false.
Now to fix that bug I talked about earlier. If you test your movie (Ctrl+Enter) after you attack you might not be able to walk or you attack might be looping or something might not be right. To fix this double click on your ‘man’ movieclip to enter it. There should now be three frames where you put your idle, walking and attacking movieclips. Click on the third frame (which should have you attacking movieclip on it, if not click on the frame it is on) and double click on the attacking movieclip. Now make a new layer and make a keyframe on the last frame and put this code on it.
Now when your man has finished attacking he will go back to his idle stance. If you test your movie now (Ctrl+Enter) It should be working fine.
Now to make your enemy. For this tutorial it is just going to be a punching bag because otherwise I would have to go into AI (Artificial Intelligence) which is more advanced.
Once you have finish making your punching bag convert it to a movieclip (F8) and give it an instance name of ‘enemy’.
Now we are going to make the health system. If you have trouble with this have a look at my Making a health system tutorial. Make a rectangle for your health bar and convert it to a movieclip (F8). Now make some dynamic text above the health bar and give it a variable name of ‘hp’.
Click on the first frame of you movie on the main timeline, and put these actions on it.
_root.hp = 100;
You might have already put stop(); there, so if you have don’t put it there agian. All that means is stop on this frame and the dynamic text ‘hp’ is equal to 100.
Now select the Hp bar and give it this code.
this._xscale = _root.hp;
if(_root.hp <= 0){
_root.hp = 0;
_root.gotoAndStop(2);
}
}
You should have already done that in my health tutorial, but I will breifly go over it again. All it means is every time this frame is entered, this _xscale is equal to _root.hp (the dynamic text). I explained _xscale before so if you forgot you should read it again. The next part says if _root.hp (the dynamic text) is equal to or less than 0, it equals 0 (so there are no negative numbers) and go to and stop on frame number 2 on the main timeline. Later on we will put a frame there, but we still need to finish the health system.
Now go back inside the attacking movieclip, and on the actions layer make a keyframe where he stops attacking (if you already made a keyframe there before, there is no need to do this). On that keyframe put this script.
_root.hp -= (random(5)+1);
}
If this is on the same frame you put actions on before just put these ones underneath the other ones. All the means is if this is touching the punching bag, the dynamic text will decrease by a random number from 0-4 +1.
If you test your movie (Ctrl+Enter) now you should see a fully functioning game. But we still have to add a 2nd frame. To do this just click on the second frame and hit F6 to make a keyframe. Now just put a message like “You killed it” or something along those lines, and maybe a “Play Again?” button. If you dont know how to make buttons have a look at my Button tutorial.
You should now have something similar to this.
I am glad to say, this is the end of the tutorial since I have been sitting here ALL DAY writing just for you guys
.
For more tutorials click here.















(Again,) Pretty good!
A few issues - you might want to prevent the guy from walking off the screen. You can either stop him at the edge or (even cooler) make a wraparound effect (which is a lot harder as well).
Try adding this to the onClipEvent(enterFrame) statement:
this._x = Math.max(Math.min(this._x,Stage.width-this._width),0);
(which assumes the guy’s registration point is top-left)
can you help me make the hero not to lose health when he walks at the punching bag or as in my case its venom.
If you use animated gifs does it make any different.
Email me at kev_branco@hotmail.com to send me some tips
This tutorial is awesome, especially with every other tutorial, I made a 2 player fighting game with this tutorial, awesome! XD
I got stuck at _root.man.gotoAndStop(1);
i put it down, but it still loops the attack!
Help!
You just put stop on all the action frames it will stop them so non replay.
that does not work. The attack loops even though you put the _roor.man.gotoAndStop(1);
same
Make sure you movieclip has an instance name of man. It is case sensitive.
hi when i play my attack loops even with the _root.man.gotoAndStop(1) in it i need help!!!!!
yea me 2
i’m not that new to flash but have no experience w/actionscript
i put stops on all the frames i needed and the goto and stop thing in the right place
i has no clu
This is a great tutorial, but i have found that
_root.man.gotoAndStop(1);
if(this.hitTest(_root.enemy)){
_root.hp -= (random(5) 1);
}
should be:
if(this.hitTest(_root.enemy)){
_root.hp -= (random(5) 1);
}
_root.man.gotoAndStop(1);
since it reads scripts top to bottom it goes to frame 1 before it calculates “damage”
at least that is how it worked on mine
Yea, you are right. But that is how I did the code in mine so it worked fine :S
Hey im sam im working on an point and click adventure game about my home town. its called welcome 2 northside. im using flash and im kinda new to gaming. can u say how to make a point and click adventure game. im having some difficultys.. if you dont know what kind of game im talking about go to…
http://uploads.ungrounded.net/.....tle=Johnny Rocketfingers 2&date=1161835200&quality=b&uj=0&w=500&h=375
I might do a tuturial on it sometime, but for now I think you should have a look at my buttons tutorial. You could make a similar but more basic game from what you learn from it.
http://www.awestyproductions.c.....-in-flash/
Can someone upload the fla version of this?
My dynamic text bar doesn’t work, i put all the codes in but when i’m testing it, nothing appears.
the bar goes down though.
1) i cant seem to fix the error where the attack gets stuck, i dont get what you mean where you say put this at the end of the keyframe.
2) how do you change the frame when the hp is 0??
3)is there a way that if the man toutches the punchbag he looses hp?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom i can help you
yo my name is kev and im almost finished a fighting game im working on where venom and spiderman fight each other. I have them attacking each other but every time they touch each other they lose health.
Can you help me!
Hoosha, to fix number 1, in the man movieclip, and then in the attack movieclip, at the end of the attack sequence/animation, on another layer (make one IN the attack sequence). Make it as long as the animation, and then, at the end, make it a keyframe.
Then add the code:
_root.man.gotoAndStop(1);
That probably doesn\’t make any sense, so here is a picture.
Notice how in the picture I am in my man movieclip, then the attack animation movieclip, and on the last frame.
—————————
Problem 2
On your health movie clip, you should have the coding:
onClipEvent(enterFrame){
this._xscale= _root.hpBar;
if(_root.hpBar
Damn character limit, email me at “kurt.single@gmail.com” and I’ll resolve this.
And the code for Problem 2 is:
onClipEvent(enterFrame){
this._xscale= _root.hpBar;
if(_root.hpBar
@Hoosha: No, otherwise people will download it and say they did it when they didn’t.
@Tom: If the bar is going down it must be working. You might have the text set to white or whatever the background color is so it appears not to be there
.
@hoosha: Do you have switch personalaties or is there two of you?
If you are having trouble email me at admin[@]awestyproductions[.]com. If you want the character to lose health when it touches the punching bag take THIS TUTORIAL.
this is so cool
Guys, when you are trying to write:
if(_root.hp <= 0){
It isn’t working because it thinks < is trying to make a new HTML tag, so use this code:
& l t ;
Without the spaces.
Wow im a very new noob and this tutorial helped me a lot thanks… In other sites they doesn’t explain so well like here
NICE it really helped me all this searching paid off ^^ Btw my email is mcdonalddarin@yahoo.com could you send me a email on wat to do to make them enemy attack you i making an naruto fighting game and i also wat him to be able to make him use justsu wen he wants can u help
@Darin: I will probably make a part 2 to this tutorial, which will include jumping and the enemy will be able to think for itself… sort of.
it will taken me about a day to figure
Hi I was having trouble at the very start
(new to flash!), making my characters!
With the men are you ment to use tweens between the different states?
THIS HELPED ME ALOT!
Thank you so very much ^_^!
How do you make your walking symbol?
can u upload just the walking *.fla file?
plz
…
How can u make the first part the picture thing..
Sorry, but you will that yourself. This tutorial is for the actionscript and how to set it up only.
ok, anyways thanks
I’m making a fighting game and it’s two player, i can’t get player 2 move but i can get it to attack with TAB to attack. But what do i put when i want to make him move left - A. and move right - D.?
Any help would be great, thank.
the code is
onClipEvent(enterFrame){
if(fight == false){
if(Key.isDown(65) && fight != true){
this._x -= 4;
this._xscale = -100;
this.gotoAndStop(2);
}else if(Key.isDown(68)&& fight != true){
this._x += 4;
this._xscale = 100;
this.gotoAndStop(2);
}else{
this.gotoAndStop(1);
}
}
if(Key.isDown(Key.CONTROL)){
this.gotoAndStop(3);
fight = true;
}
if(this._currentframe == 1){
fight = false;
}
if(Key.isDown(Key.TAB)){
this.gotoAndStop(4);
fight = true;
}
if(this._currentframe == 1){
fight = false;
}
if(Key.isDown(Key.ESCAPE)){
this.gotoAndStop(5);
fight = true;
}
if(this._currentframe == 1){
fight = false;
}
}
I also got stuck at _root.man.gotoAndStop(1);. My man’s name is right and I’ve put the script on a new layer on the last frame in the attacking movie clip. What do I do?
I just solved this problem by clicking on the instance of the man and typing his name in the properties box to the left under the drop down meu that read ‘movie clip’ Silly me!
You really did a wonderful job. It made me inspire to continue learning flash which i stop couple of years ago. Do you have a website where your tutorials and samples can be found. Tnx you!
@Tom: For keys other than the arrow keys, instead of doing something like Key.isDown(Key.LEFT) you change it to this:
Key.isDown(KeyCode);
Since the key ‘W’ key code is 87 you would put
Key.isDown(87);
You can find a full list of key codes here:
http://www.awestyproductions.com/images/keycode.txt
@Artechx: You are on it right now. To see a full list of tutorial go here:
http://www.awestyproductions.com/tutorials/
Thanks =)
Do you know how to make items break, say i attacked a box and it broke?
Thanks for the help earlier.
I need help with my health, when it goes down to 0, it sends me to the main menu on scene 1.
I want it to send me too Scene 2, Frame 3.
How do i write it, because this didn’t work:
_root.gotoAndStop(”Scene 2″, “3″);
Any help would be great, thanks. =D
Thanks, this taught me alot of useful action scripting in very short time.
But I’d also like to know how to make the punching bag (or box in my case) break.
Furthermore, lets say I wanted a wall and when the punchingbag’s hp reaches 0 the wall will either disappear or move away allowing the character to move on. How would I go about doing that?
Also Sam, Im really interested in that point and click adventure game you were talking about. Can you make a tutorial when your done? Also I went to see the web site you linked and it was down, could just be my browser.
Thanks a lot! Rick said everything I was going to say
@Tom: You only need to put strings (A word, like Scene 2) in quotation marks (”"), not integers (a number). So it would be:
_root.gotoAndStop(”Scene 2″, 3);
About the breaking items, I will do that in another tutorial.
@Rick: Read the last part of what I wrote for tom and I will try and make a point and click tutorial.
Also try this link:
http://www.newgrounds.com/portal/view/310635
Okay, Thanks. =]
I’m making a AI computer chacracter but when it comes towards me it doesn’t walk, i made it in a movieclip like the way i would make a player one character, i’ve used this coding;
if(rxerx 40)
this._x =2;
_.root.boltwalk.play();
}
if(_root.man.hitTest(this))
_root.boltattack.play();
}
but he just slides and when it says; “_root.boltattack.play();”
It doesn’t do anything.
Well set him up like how we did to the character in this tutorial, and instead of saying _root.boltwalk.play(); do this.gotoAndStop(2); which would have the walking MC on it, and do the same for the attack.
Could you give me a simple script one enemy AI in a fighting game. Just to have him randomly attack, and advance to attack?
I will try to do a tutorial before the weekend.
Sweet! Thanks you so much!!
My man now attacks but doesn’t walk, can you help me with the coding for my enemie:
onClipEvent(enterFrame)
{
distance=600
rx=_root.man._x
ry=_root.man._y
erx=_root.bolt._x
ery=_root.bolt._y
if (Math.sqrt( (rx-erx)*(rx-erx) (ry-ery)*(ry-ery))erx 40)
this._x =2;
_.root.bolt.gotoandstop(2);
}
if(_root.man.hitTest(this))
_root.bolt.gotoandstop(3);
}
He doesn’t face right when i run behind him either, what coding do i need to make him do this?
when i try to replace the > in
if(_root.hp
with & l t;(i dont no y my last post got cut off) i keep getting an error
Im new to flash and I wanted to know… what do you mean by: (in brakets)
You need to make a movieclip of him standing still, walking/runnning and punching/kicking/attacking.
When you have got all of these I want you to put them all into one ((single movieclip)). With the standing movieclip ((on the first frame, the walking/running on the second and the attacking on the third.))
@Tom: I have seen that code before.
It is from another tutorial. Anyway, try changing it to this:
onClipEvent(enterFrame){
distance=600;
rx=_root.man._x;
ry=_root.man._y;
erx=_root.bolt._x;
ery=_root.bolt._y;
if(Math.sqrt((rx-erx)*(rx-erx)(ry-ery)*(ry-ery))erx 40){
this._x -= 2;
_root.bolt.gotoandstop(2);
}
if(_root.man.hitTest(this)){
_root.bolt.gotoandstop(3);
}
That might work, but it is untest and I am not sure about that trigg.
@Strangen: I think you might need to be a registered member to do that, so just replace < with [ or something.
@Billy: I would recommend taking some more basic tutorials first.
:’( Am Super |\|008 at this and ive been staying up all night for like weeks trying to figure this out :(:(:( but how do i make it so that it goes to another frame when my enemy has 0 hp??
It says that in the tutorial does it not?
…You should have already done that in my health tutorial, but I will breifly go over it again. All it means is every time this frame is entered, this _xscale is equal to _root.hp (the dynamic text). I explained _xscale before so if you forgot you should read it again. The next part says if _root.hp (the dynamic text) is equal to or less than 0, it equals 0 (so there are no negative numbers) and go to and stop on frame number 2 on the main timeline. Later on we will put a frame there, but we still need to finish the health system…
…But we still have to add a 2nd frame. To do this just click on the second frame and hit F6 to make a keyframe. Now just put a message like “You killed it” or something along those lines, and maybe a “Play Again?” button…
im making a birds eye view rpg
and i wanted to learn how to make
him do the walk cycle in all directions thnx for the help…nice tutorial
It still repeats the Attack even after I put
_root.man.gotoAndStop(1);
is there somthing I missed?
@ Hoosha the Picture is gone? do you have another one?
Thanks, but it still doesn’t work xD
@Ghuiado_thenewbieinflash: Where did you put it?
There was no picture.
@Tom: Just send it to me other wise wait for the AI tutorial
admin[@]awestyproductions[.]com
hey i was reading this tutorial helped me so much and like others here i was wondering about A.I. and how to make him jump and controll him in the air after he has jumped. So i was wondering how the tutorial is going it would help me so much!
; )
i tried replacing it with [ but im still getting and error
No, lol. I meant replace it in the comment because otherwise it cuts off so I can’t see the rest of the error. It will not work if you do it in flash.
Okay, Thanks
we took three different animations and put them into one movieclip on three different frames. When I put the code in when he went from standing to walking…. there is a mild gap between the animations…. do you have to place the animations on top of each other put in different frames?
I noticed if you hold space, the character continues to attack. This is inconvenient for quick attack animations. How should I make it so you have to press the key for every attack?
Hey thats the best walking loop ive ever seen!
You should do a tutorial to make it!!!
@onyx: Yes, different frames, same spot and make sure the all the frames have stop(); one them.
@MrMister: You would have to change alot of the code which I do not really feel like doing at the moment so all I can suggest is if it is a quick attack make it only take of 1 or something. But for a long attack make it take of 10.
@Keiren: lol, thanks. I might make tut about it, but not now
i had another problem… my animations are really long because of an item in his hand. when i do the _xscale = -100; it jumps way off the spot he should be turning at… How do I turn on a different pivot point. I tried adding new frames with a symbol of the same animation of walking just turned left… but I couldn’t figure out the code…. could you help??? is there an email I can write to for help? (By the way… these tutorials are fantastic!)
How would you make it so the MC will only play a motion tween such as “Walking” when the player only presses Key.LEFT, or Key.RIGHT?
Here is what I have created with your Tutorials.

http://img101.imageshack.us/my.....ametq9.swf
Thanks I learned alot of Basic AS!
@Onyx: Try changing the registration point. When you make a MC, you can choose the registration point. But afterwards you have to change it manually.
When you double click on your MC and go into your movieclip you should see a little cross, that is the registration point (the pivot point). Move whatever is inside that MC so the registration point it in the center.
@Slash: Is the motion tween in a symbol or on the main timeline?
@macrodia: Nice. That is really cool. If you don’t mind me asking where did you get those sprites from, they are awesome.
hey awesty im havin a prob when i move left he goes but then when i push right he come some where out of th right then start running wat do i do
I didn\’t understand what you said then. But it sounds like the same problem as onyx.
@awesty: I got the sprites frome http://mslugdb.com/ they had a great collection of metalslug sprites. Only Problem is the site is Defunct now.
<But I do have a large collection of them already on my HD.(: