07.01Create a Simple Turn-Based Game
Well I’ve noticed my tutorials seem very complicated and long-winded, so here’s a simple tutorial for you beginners!
Lets create a new document at 30 FPS, and draw two characters. Turn them both into symbols and give one the instance name of “player” and the other “enemy”. Now underneath them we’ll need four buttons: attack, magic, item and run. Then give them the exact same instance names.
Firstly we’ll need to organise turns. Therefore we can only do stuff during our turn. We also want to make variables for whether we’re attacking or using magic etc. Oh, and add two text boxes - one with the Var of “player_health” and the other with “enemy_health”. Now put this in your actions!
var players_turn:Boolean = true; //whether its the player's turn or not var player_attack:Boolean = false; //whether the player is attacking or not var player_magic:Boolean = false; //whether the player is using magic var player_item:Boolean = false; //whether the player is using an item var player_run:Boolean = false; //whether the player ran away! var player_health:Number = 10; //our player's health var enemy_health:Number = 8; //the enemy's health function onEnterFrame() { if (players_turn) { if (player_attack) { //if the player is attacking enemy_health--; //damage the enemy player_attack = false; //the attack is over players_turn = false; //the turn is over } else if (player_magic) { //if the player uses magic enemy_health -= 2; //damage the enemy a little bit more player_magic = false; players_turn = false; } } else { //when it's the enemy's turn switch (random(2)) { //for now we're going to randomly make them attack or use magic case 0 : player_health--; //hurt the player players_turn = true; //it's now the player's turn break; case 1 : player_health -= 2; //hurt them even more! players_turn = true; //it's now the player's turn break; } } } attack.onRelease = function() { //when our attack button is clicked if (players_turn) { //if it's our turn player_attack = true; //attack! } } magic.onRelease = function() { if (players_turn) { player_magic = true; } }
Well, we’re definitely fighting the enemy, but it’s not very interesting. Let’s increase the health and make the damage a bit more random! Another essential thing is some animation. Firstly add a stop ( stop(); ) action on the first frame of your player. Then make the second frame an attacking animation. Make that a movie clip and give it the instance name of “attack_mc”. Then on the third frame make a movie clip of the player doing magic and give it the instance name of “magic_mc”. Now do the same for enemy (with the same instance names). Here’s our code!
var players_turn:Boolean = true; //whether its the player's turn or not var player_attack:Boolean = false; //whether the player is attacking or not var player_magic:Boolean = false; //whether the player is using magic var player_item:Boolean = false; //whether the player is using an item var player_run:Boolean = false; //whether the player ran away! var player_health:Number = 100; //our player's health var enemy_health:Number = 80; //the enemy's health var enemy_attack:Boolean = false; //whether the enemy is attacking or not var enemy_magic:Boolean = false; //whether the enemy is using magic function onEnterFrame() { if (players_turn) { if (player_attack && player.attack_mc._currentframe == player.attack_mc._totalframes) { //if the player is attacking enemy_health -= 10+random(10); //damage the enemy player_attack = false; //the attack is over players_turn = false; //the turn is over player.gotoAndStop(1); //player look normal again } else if (player_magic && player.magic_mc._currentframe == player.magic_mc._totalframes) { //if the player uses magic enemy_health -= 3*random(10); //damage the enemy with a bit of risk player_magic = false; players_turn = false; player.gotoAndStop(1); } } else { if (!enemy_attack && !enemy_magic) { //when it's the enemy's turn switch (random(2)) { //for now we're going to randomly make them attack or use magic case 0 : enemy.gotoAndStop(2); //enemy go to attack frame enemy_attack = true; //enemy is attacking break; case 1 : enemy.gotoAndStop(3); //enemy go to magic frame enemy_magic = true; //enemy is using magic break; } } if (enemy_attack && enemy.attack_mc._currentframe == enemy.attack_mc._totalframes) { player_health -= 10+random(10); //hurt the player players_turn = true; //it's now the player's turn enemy_attack = false; //no longer attacking enemy.gotoAndStop(1); //make the enemy go to normal frame } else if (enemy_magic && enemy.magic_mc._currentframe == enemy.magic_mc._totalframes) { player_health -= 3*random(10); //hurt them with risk! players_turn = true; enemy_magic = false; //no longer using magic enemy.gotoAndStop(1); } } } attack.onRelease = function() { //when our attack button is clicked if (players_turn && !player_attack && !player_magic) { //if it's our turn, and we're not attacking or using magic player_attack = true; //attack! player.gotoAndStop(2); //our attacking frame } }; magic.onRelease = function() { if (players_turn && !player_attack && !player_magic) { player_magic = true; player.gotoAndStop(3); //our magic frame } };
Looks good. Nothing special, but it’s simple and easy to build on. Maybe there’ll be a part 2…

Very Useful for beginners!
I found your blog yesterday, and I am very impressed. 
PS:
Email me…email found here.. eaglevision.890m.com/email.php
Thanks.
EV
July 2nd, 2008 at 1:50 pm
Nice tutorial… No… Amazing tutorial…
=D Keep like this…
July 30th, 2008 at 7:25 pm
how the hell do you do that??? i sort of need ALOT of help………….. im just a beginner but i find that hard. i need more detailed instructions:(
August 6th, 2008 at 2:01 pm
This is an excellent tutorial!
August 7th, 2008 at 6:08 am
Bloodyboy, what part are you struggling with?
August 8th, 2008 at 12:27 pm
I’m having trouble with the text box var I’m not sure what you mean.
August 9th, 2008 at 12:52 am
sorry never mind i worked it out i was using action script 0.3
August 9th, 2008 at 1:06 am
ok this is kinda messed up! i need help with the bit about putting animations in the attack. Could you maybe exsplain a bit more CLEARLY its a bit messed up for beginners lol. its all so confusing.
August 10th, 2008 at 11:04 am
Chris, basically you just animate it yourself inside the movie clip. Have a look at the source files to help you understand.
August 10th, 2008 at 1:41 pm
The point of the items and run buttons was…?
August 18th, 2008 at 1:05 pm
… for future parts?
August 19th, 2008 at 3:41 pm
it is very usefulflah learners and also experts.i sugest it is give simple language
October 4th, 2008 at 5:25 am
what about the item and run?
i’m kinda messed upt here….
October 27th, 2008 at 7:19 am
Heey when i saw this tutorial i tried to make my own battle.
eventualy when i finished it it didnt work everything wrks exept for the animation of the enemy magic.
E-Mail : quincy_1994@live.nl
Could you send me a email and then il send my fla to you cuz i have tried in a forum but nobody knows why
November 11th, 2008 at 7:10 am
Quincy, check out the source files and compare them. Make sure all your instance names are right, too.
November 11th, 2008 at 11:49 am
hey there.. just finished the tutorial… but once i tried playing again none of the animations took effect. it was the same as the first code prettymuch… email me and help me out
November 19th, 2008 at 8:28 am
How do you give the buttons instance names?
December 5th, 2008 at 6:29 pm
How do you make an animation in ONE frame? I downloaded the source and I don’t see where stuff moves!
March 25th, 2009 at 11:21 am
Bob, Click on the movie clip, the animation is NESTED within
May 15th, 2009 at 7:51 am
How would I end the fight when either the enemy or the player wins
May 28th, 2009 at 7:02 am