Turn Based Combat
In this tutorial you will learn how to make a turn based combat system. I am using Flash 8, but Flash MX 2004 will work aswell.
Click here to see an example of what we will be making.
You need to have some knowledge of flash before you take this tutorial (MovieClips, Variables, All the tools etc.)
First of all, you need to make your character. Make one MC (MovieClip) with the character standing still, and the other with it attacking. Now, most people have trouble understanding this, but you have to make a blank MC (Ctrl+F8). On the first frame of that MC, drag the standing still MC from the library (Ctrl+L) onto the stage. On the second frame drag the attacking MC from the library onto the stage. Make sure they are in the same spot. Put the action ’stop();’ on each of the frames. Now return back the the main timeline and give that MC an instance name of man. (It is case sensitive)
Now do the same thing, but for the enemy. Give this an instance name of enemy.
Now make a new MC (Ctrl+F8). Leave the first frame blank. On the second frame, have something like this:
Give the dynamic text an variable name of healthPotion.
And on the third frame have something like this:
Give this an instance name of attack.
Now make one more MC. This will be the HP bar. Put one underneath the man MC and one underneath the enemy MC.
Above those HP bars put some dynamic text.
Give the one above the man MC HP bar a variable name of hP. Give the one above the enemies HP bar a variable name of ehP.
Now for the code. ;D (Make sure you read all the comments).
Put this code on the man MC.
onClipEvent(load){
//The _global variable ‘turn’ is true
_global.turn = true;
//The _global variable ‘waiting’ is false
_global.waiting = false;
}
//When this frame is entered
onClipEvent(enterFrame){
//If turn is true and waiting is false…
if(turn == true && !waiting){
//the text goes to the 2nd frame (Choosing your attack)…
_root.attack.gotoAndStop(2);
//unless waiting is true…
}else if(waiting == true){
//the text goes to the 1st frame (blank)…
_root.attack.gotoAndStop(1);
//otherwise…
}else{
//the text goes to the 3rd frame (enemies turn).
_root.attack.gotoAndStop(3);
}
}
Put this code on the enemy MC:
onClipEvent(load){
//Declares a new variable called waitTime which has the value of 10
var waitTime:Number = -10;
}
//When this frame is entered
onClipEvent(enterFrame){
//if the variables ‘turn’ and ‘waiting’ are both false…
if(!_global.turn && !_global.waiting){
//This MC goes and stops on the 2nd frame…
this.gotoAndStop(2);
//and waiting is true
waiting = true;
}
}
Put this code on the man MCs health bar.
onClipEvent(enterFrame){
/*The _xscale of this MC is equal to hP, the max is
0 (it won’t go and lower), and the min is 100 (it
won’t go any higher)*/
this._xscale = Math.max(_root.hP,0);
this._xscale = Math.min(_root.hP,100);
//If hP is greater than 100…
if(_root.hP > 100){
//It equals 100
_root.hP = 100;
}
//If it is less than 0…
if(_root.hP < 0){
//It equals 0
_root.hP = 0;
}
}
Put this on the enemies HP bar.
this._xscale = Math.max(_root.ehP,0);
if(_root.ehP > 100){
_root.ehP = 100;
}
if(_root.ehP < 0){
_root.ehP = 0;
}
}
That is exactly the same as the other one, except for a few variables.
Put this on the attack button in the attack MC.
on(release){
//the man MC goes the the 2nd frame…
_root.man.gotoAndStop(2);
//And waiting is true.
_global.waiting = true;
}
And put this on the Health potion button in the attack MC.
on(release){
//If there are some potions left…
if(_root.attack.healthPotion > 0){
//Turn is false…
_global.turn = false;
//Waiting is false…
_global.waiting = false;
/*Hp increasing by a random number
between 0 and 29 with an additional
20 added on. So the lowest is 20,
the highest is 49.*/
_root.hP += random(30)+20;
//The man MC goes and stops on the 1st frame.
_root.man.gotoAndStop(1);
//The No. of potions decrease by 1.
_root.attack.healthPotion -= 1;
}
}
Put this on the last frame on the man MCs attacking MC.
_global.turn = false;
//Waiting equals false
_global.waiting = false;
//The enemies hP decreasing by a number
//between 3 and 9
_root.ehP -= random(7)+3;
//This MC goes to the first frame
_root.man.gotoAndStop(1);
Put this on the last frame of the enemies attacking MC.
_global.turn = true;
//waiting is false
_global.waiting = false;
//the man MC’s HP decreases by a number
//between 5 and 16
_root.hP -= random(12)+5;
//This MC goes and stops on the 1st frame
_root.enemy.gotoAndStop(1);
And finally, put this code on the first frame of the main timeline.
_root.ehP = 100; //The enemies " "
_root.attack.healthPotion = 2; //Change this to however many potions you want.
Now you should have a fully functioning turn based combat system.















great tutorial! I’m going to use this ing my rpg game for battles!
Can you posibly make a tutorial like this where you can play two play? Because that would be really awsome!
This is by far your best tutorial ever. I can impliment it into any game! You’re the best!
Actually I went back to make it, and when I did, the enemy won’t stop attacking, and I can’t use potions.
Make sure you follow the tutorial closely. Make sure you haven’t missed anything.
Hi awesty. That tutorial was amazing. Probably one of the most useful youv’e done so far. But i think there is a slightly easier way to do it just using if statments and MCs, anyway i’m not critizing you. I have another question to ask
Is your next tutorial going to be one about a GTA type game? because that would be the best one yet.
When are you going to start posting the fla files?
@Bobkins: Ah… Im not sure what it will be.
@asdf: When I feel like it
This tutorial was coool!!!!
I have the same prob as Bob. But I went through everything and read it (you’d b suprized if you know me, i don’t read notes)
hi thats a good tutorial. I was wondering - how can you tell if your game is going to take time to load and needs a pre loader? My games are roughly 3mb, and they load for me. Im not sure if they’ll load on the net fast, so i think i need pre loaders. Any help? thanks.
i forgot to ask another question i had errors coming up with some variables on another project. It keeps telling me to add ( somewhere but i dont know where. The errors are like this:
**Error** Symbol=turn, layer=Layer 1, frame=2:Line 6: ‘(’ expected
if _root.wait = 5; {
**Error** Symbol=turn, layer=Layer 1, frame=2:Line 11: ‘(’ expected
if _root.wait = 10; {
**Error** Symbol=turn, layer=Layer 1, frame=2:Line 7: ‘(’ expected
if _root.wait = 10; {
**Error** Symbol=turn, layer=Layer 1, frame=2:Line 12: ‘(’ expected
if _root.wait = 5; {
Total ActionScript Errors: 4 Reported Errors: 4
I tried putting it like this
(_root.wait
and
((_root.wait)
etc but it wants it somewhere else. Can you tell me where to add it?
hello awesty that was a fab tutorial and very very useful. I need to ask a simple question, when you add variables you can make it add a random ammount. Can you please tell me the code to put on a frame that would make another mc play a random frame no. on its own timeline? Ta again awesty.
@Pamela: I cant really help you anymore than that. Unless you send it to me.
@Dragonguy: 3mb is alot, so add a preloader. If it is the .fla that is 3mb, that .swf wont be as much. Also, with your problems, change them all to this:
if(_root.wait == 5){
and
if(_root.wait == 10){
@KatyS: _root.MC.gotoAndPlay(random(NUMBER) 1)
Replace NUMBER with the number of frames there are. It has 1 because if you have random(10), it is from 0-9. Replace MC with the instance name of the MC.
hello again. I tried adding to the random frame code, so that it would etheir play at 1,20,or 40. But i couldnt find a way and there is an error with the normal code you sent me :S
**Error** Symbol=man, layer=mantween, frame=35:Line 2: ‘)’ or ‘,’ expected
_root.enemy.gotoAndPlay(random(84) 1)
i would be grateful if you could help. Thanks.
woohoo! this is the best tute yet!!
but i have a question…you see, i made another type of atk called man_magic_atk, and it is in the third frame of the man mc, and each time i use it, the MagicBar(a dynamic text use to cast magic spells) will go down, i completed that part but if the magic bar hits zero, i can still use my magic, how can i not use it when the MagicBar hits Zero?
thnx awesty
@KatyS:
randomFrame:Array = [\”1\”,\”20\”,\”40\”];
i = random(3);
_root.enemy.gotoAndPlay(randomFrame[i]);
That should work, but it is untested.
@KIM: You will have to make it the same as the health potions.
if(MAGICTXT > 0){
//ATTACK
}
So if it is below, it wont attack.
ahh!… once again you have helped me from my request!
thank you almighty awesty
In the demo the healthbar on enemy doesnt work and it goes into negative numbers…
oops… >_<’.
But it should still work in the tutorial.
it just says syntax error
hey, how can i make my “man” defend?
(like it can’t be hit when i picked the button “defend” and it stays in that frame and after the enemy moves, the “man” will go back to frame 1(standing frame)?
how can the enemy not hit me when i click the defend button?
@KatyS: You need to remove the backslashes and retype the qoutation marks.
@KIM: Well, make a variable that is true when the man is defending. And on the code where it takes off HP, put this around it:
if(myVariable != true){
}
So if it is true, it wont decrease in health.
hmm…. i tried it but it won’t work. I also tried rearraging the brackets etc but no luck
hey there!
gr8 tutorial but i have one question,
how do i make it so that once all of either sides HP are gone it will stop the game mabye with a win/lose message?
@KatyS:
randomFrame:Array = [’1′,’20′,’40′];
i = random(3);
_root.enemy.gotoAndPlay(randomFrame[i]);
Dont copy and paste it, write it straight in.
@zionaise:
if(_root.hP < 0){
_root.gotoAndStop(FRAME);
}else if(_root.ehP < 0){
_root.gotoAndStop(FRAME);
}
Hey Awesty, best 1 yet, maybe you could make a second one and when we hit the enemy it flinches that whould be a lot better
other than that its kool
err.. i trust that ur code will work but i cant find what to put it on =( plz help!
Hey Awesty,
Please o please check out my (mini) game its really kool, and i think you whould be pleased, the only thing i need help on is making an Xp bar, so when you kill an enemy it gives you xp then when you get a certain amount of xp you level up
check my game out!!
http://www.swfup.com/swf-view.php?id=4871
@Bryce
For the xp thing, if your guy has a health bar then this would be the code..
onClipEvent(enterFrame){
this._xscale = _root.VAR;
if(_root.VAR
nice try with the game bryce, i say the only thing would maybe make it more spaced out and improve the dodgey animation
i have a problem and was wondering if anyone could help. I want it so that when you click a button that is inside an MC, the button disapears and an MC appears inside an MC (the inventory) which can be displayed by clicking a button.
But it doesn’t work. It makes the button disapear, but the item doesnt appear in the inventory when i click the button to open up the inventory mc. Iv’e got alpha and all that in, but i don’t no y its doing nothing.
hey can someone tell me the code for when you click a button it removes the button? just like you would remove an MC and it doesn’t respawn or u dont see it again throughout the movie
i have a big problem and its seriously frustrating me. I want it so that if you click a button, and a variable equals something, then it will subtract variables and add some, and then make an MC called unlocked play at frame 2. I used this code but its not working
on (release) {
if(_root.task1 = 5 )){
_root.money =20;
_root.energy -=20;
_root.d = 5;
_root.slot1 = (”")
_root.text.gotoAndStop(3);
_root.unlocked.gotoAndStop(2)
_root.mana =5;
_root.task1 = 1000
}else _root.money = 0;
_root.energy -=0;
_root.mana =0;
_root.text.gotoAndStop(2)
_root.unlocked.gotoAndStop(1)
}
slot1 is a variable. Thanks if you can help
it didn’t work for me, couldn’t u put ur “.fla” file? so we can download it from somewhere..
I dont understand why you declare the waitTime variable..?
otherwise, this is a great tutorial!
great tutorial. some questions tho, how can i make it so u jump to “X” frame when health reachs 0, (gameover frame) and a “continue playing” frame if u win?
and any chance u can make an “XP” system and “buying items” system, since it shouldnt be that long.. i would love u to mail em to me on Seerex11@hotmail.com.. thank you
and any chance u can make an “XP” system and “buying items” system, since it shouldnt be that long.. i would love u to mail em to me on robinweaver@san.rr.com.. thank you
LOL
plz some1 gimme link so i can download it, cuz i cant get it working..
and how can i do that? whats the code?
ok then if u dont wanna put the file on internet
Plz tape when u make the tut from start to end so i understand exacly.. and write in middle of the movies and tell what u do, set it on Youtube or Video.google or something
i really want this game.. plz help! ^^
gimme the .fla file plz.. i cant get it working..
i get 8 errors =S
and it stays when i attack.. nothing happends..
**Error** Scene=Scene 1, layer=Layer 1, frame=2:Line 2: Statement must appear within on/onClipEvent handler
_global.turn = false;
**Error** Scene=Scene 1, layer=Layer 1, frame=2:Line 4: Statement must appear within on/onClipEvent handler
_global.waiting = false;
**Error** Scene=Scene 1, layer=Layer 1, frame=2:Line 7: Statement must appear within on/onClipEvent handler
_root.ehP -= random(7) 3;
**Error** Scene=Scene 1, layer=Layer 1, frame=2:Line 9: Statement must appear within on/onClipEvent handler
_root.man.gotoAndStop(1);
**Error** Scene=Scene 1, layer=Layer 1, frame=2:Line 2: Statement must appear within on/onClipEvent handler
_global.turn = true;
**Error** Scene=Scene 1, layer=Layer 1, frame=2:Line 4: Statement must appear within on/onClipEvent handler
_global.waiting = false;
**Error** Scene=Scene 1, layer=Layer 1, frame=2:Line 7: Statement must appear within on/onClipEvent handler
_root.hP -= random(12) 5;
**Error** Scene=Scene 1, layer=Layer 1, frame=2:Line 9: Statement must appear within on/onClipEvent handler
_root.enemy.gotoAndStop(1);
Total ActionScript Errors: 8 Reported Errors: 8
@Zionaise: It tells you in the tutorial.
@Bryce K: Nice work. For an XP bar, try making a variable that increases when you win. Then when you use the XP for upgrades or something, it decreases.
@no1jock: Maybe try making the button take the MC to a dif frame, where the button isnt there but the item is.
@KatyS: Use an MC instead of a button. Give it and instance name of \”remove\”. On the frame, put this code:
remove.onRelease = function(){
this.swapDepths(9999);
this.removeMovieClip();
}
That should work.
@dragonguy: Try this:
on (release) {
if(_root.task1 == 5 ){
//I am not sure if that is meant to be += below
_root.money =20;
_root.energy -=20;
_root.d = 5;
_root.slot1 = (”\”)
_root.text.gotoAndStop(3);
_root.unlocked.gotoAndStop(2);
_root.mana =5;
_root.task1 = 1000;
}else if(_root.money == 0){
_root.energy -=0;
_root.mana =0;
_root.text.gotoAndStop(2);
_root.unlocked.gotoAndStop(1);
}
}
@Aspherin: Whoops, I was supposed to take that out. Origanally the enemy waited a bit before it attacked, but it screwed up so I left it out.
@seerex:
if(_root.hP <= 0){
_root.gotoAndStop(FRAMENUMBER);
}
if(_root.ehP <= 0){
_root.gotoAndStop(FRAMENUMBER);
}
@Mike: Why dont you follow the tutorial like everyone else? If you have trouble doing it, it is probably to hard so you should try easier things first. I am not going to record it since alot of the tutorial is coding, and it is easier just to copy and paste it then read the explanations then trying to read the text out of the recording (which would be hard because it would be bad quality).
Also most of the errors you posted are because you put the code on an MC when it should be on a frame.
it dosen’t work!
i cant get my man defend!
Just keep experimenting with it.
Thanks man
MUAHAHAHHA! Im kinda polished my game a bit tell me wut u guys think
(Campain is not even close to done :P) Tell me wut ur guy’s highest score is
http://www.swfup.com/swf-view.php?id=4989
14 or so but i was healing from then on so i stopped i was a worrior. good game so far mine need alot of work.
Bryce K, ultra nice game ^^ =)
i still want a .fla file plz =(
I got 6.
When you beat the pac-bird in campaign and upgrade your stats, it goes back to survival mode.
guys, i got away from the errors, but still not working =(!
plz some1 make one with ugly characters so u dont think i’ll steal your game, and send me the .fla file.. i cant make this but i really want!
when i attack nothing happends and when i use health potion nothing hapends, so, no1 attack, so i cant play my own game =/
well, this just sound noobish, but at the XP thing, u said make a variable that incraese and decrease so on.. and how do i make a variable? sry i sound like a noob, i get ur tutorial here working fine, but how do i make an EXP system?
secondly, when i need to jump to desired frame (or SWF file) when my man touchs target? also, how do i make a button change movie, so the whole game doesnt have to be in one big SWF file?
thanks alot for helping
uh sry forget a question.. very sry for all the questions there mate, but well, ehm yeah… im making my own RPG game and i want it to be extremely good… with help from you ofc, u will also be in the credits..! for sure!
but well, about getting quest’s from an NPC, any way to make it give u XP when completing that Q, or maybe even a reward? how u increase damage done when equipping sword? all i think of is to change movieclip to another one and give higher ATK.. and finally, how to throw out a weapon? =) thank you
sry to bother u AGAIN, but i always forget to input the last questions.. FINAL question for now.. how do i make an event accour (when man touches wall it jumps to fram “X” and plays, like he enters a new zone)?
plz help me awesty!
Thanks for telling me that awesty
@Seerex Hey i can answer your last question, (with the help of awesty’s hit test TUT) First do the TUT so you understand hit tests. Second make a BUTTON wall, (oh lets say to the right) Now put this code on it (IT HAS TO BE A BUTTON!!)
on (keyPress “x”) {
if(this.hitTest(_root.SOMETHING)){
_root.gotoAndPlay(FRAME);
}
}
NOTE: Fill in SOMETHING and FRAME with a Movieclip and a Keyframe
Oh yah also Seerex you might want to put a wall behind/ on top of the button because this wont stop the MC, it will go right through it
hey give awesty a break and try to work things out yourself, and that way you’ll learn better.
ya i agree with rdb. awesty works hard and thats when he’s not at school! :p
how can you make when enemys health is 0 you win &
when your hp is 0 you lose?
thanks alot bryce.. and yeah i know, i didnt think much myself there, sry awesty man, and btw, phil, awesty already answered that question, look up…
ehm bryce, it gives this error when i add the code to the button/wall thing
Clipboard Actions: Line 1: Key code identifier expected
on (keyPress “x”) {
any ideas?
and bryce, are u sure u get my question right? i mean, i would like a wall, ill make its alpha 0, but when u hit that wall it will transfer you to target frame… what i dont understand bout ur code is the on (keyPress “x”) {
dont need a keypress, but when he touches wall it transfers him.. please help me mate.. and awesty, if u have the time i would love answers on the other questions… thank you.. sry for being a pain in the ass, very good work for someone ur age, ur far better than me =).. good work mate!
Yah, okay as i said you make it a button, one with that action and the make a movieclip with the hit test action, wut my action does is when you press X it will jump to another frame
@Phil try to learn it for urself but if you must know at the end of your hp bar actionscript before the } put this code
gotoAndPlay(FRAME);
do the same with the enemy code
understandable bryce, but can i get ur e-mail please? would be very nice, since i think i can learn alot from u if u dont mind…
thank you
ok… i’ll try
@Mike: If you cant do it, it is probably to hard for you. Just do something easier until you understand this one.
@Seerex:
Variables: http://www.awestyproductions.c.....ables-101/
@Phil: Read previous comments. I feel like I have answered that question 100 times! (Thats including all the tutorials).
About making a game a bunch of different .swfs, I wouldnt recommend that.
About increasing stats… You could have a damage variable, say its called damage and it is equal to 5. So when you attack, you can just put:
_root.hp -= damage; //Which is 5
When you upgrade or something, the damage variable increases. So then the code would still be:
_root.hp -= damage;
But damage would be a higher number.
When you complete a quest, say your gold or XP was a dynamic text box. So at the end of the quest you could have something like:
if(questComplete){
_root.xP = 50;
_root.gold = 250;
}
Or something like that.
Also, no offence Bryce, but I wouldnt use that code. I would do something like:
onEnterFrame = function(){
if(man.hitTest(wall)){
_root.gotoAndStop(X);
}
}
Put that on the main frame, with the character the moves having an instance name of man, and the wall an instance name of wall. Change X to the frame you want it to go to.
ok ok, i give up about this defend thingy!
anyway….
thanks alot matie, ill get started reading about the variables right away!
ehm awesty, not sure if i understand fully.. u mean add _root.hp -= damage; //5 and add another _root.hp -= damage; on the weapon when he picks it up? to increase damage or?
and ur quest complete thing, where do i put it, and how does it know its a quest complete thingy?
maybe send an FLA file to Seerex11@hotmail.com, would be nice
ehm, this is driving me nuts, saw u told Seerex the thing with the onEnterFrame = function(){
if(man.hitTest(wall)){
_root.gotoAndStop(X);
}
}
well, i used this code as onEnterFrame = function(){
if(man.hitTest(village)){
_root.gotoAndStop(396);
}
}
so when it hitted the wall village, it should take me to fram 396, but instead take me to a frame about 190-200? ive used 30 min to figure it out but i just cant..?
can it have something to do with the fact im using scene 5 for the game? if so, whats the code used for another scene?
oh, silly me, got it now hehe…
hey again awesty, i promise this is the last time ill bother you, but its