First time here? First time here? Newsletter Signup Newsletter
Login | Register | 651326 Members and growing!
Search tutorials
Author: Oliver Sale | Website:
I started making this script with the idea of having multiple enemies on screen and the user clicks on the enemy they want to attack. To do this I made it so the enemy is selected when you click on it and you can see it's stats that way, when you click on the enemy again it clears the stats, this is done by the following script: //Monster Details _root.enemy_mc.onRelease = function() { if (_root.enemy_mc.chosen == 0) { _root.attack_btn._visible = true; _root.enemyHealth_txt.text =; _root.enemyLevel_txt.text = _root.enemy_mc.level; _root.enemyName_txt.text = _root.enemy_mc.nick; } else { _root.attack_btn._visible = false; _root.monsterHealth_txt.text = ""; _root.monsterLevel_txt.text = ""; _root.monsterName_txt.text = ""; _root.clearInterval(enermy1interval); } }; Now then, what you've got so far is the setup for the fight 'engine' if you will, the following script is the fight sequence: _root.attack_btn.onRelease = function() { // //Fight Interval Revisited! //Character Attacking Enemy _root.fightStat_txt.text += "You're Initiating attack\r"; //prints the text to the fight stats text field _root.fightStat_txt.scroll +=1; //gets the fight stat text field to scroll up one after printing the previous text _root.enemy_mc.attackInterval = setInterval(function () { //The start of the chatacter attacking interval if (>0) { // if the enemies health is above "0", continue the sequence if (random(_root.character_mc.attack+_root.enemy_mc.defence)<=_root.character_mc.attack) { // this is the chance of hit, it's worked out by the characters and attack and the enemy's defence added together then the ammount of the character's attack is the chance of hitting -= (random((_root.character_mc.maxAttack-_root.character_mc.minAttack)+1)+_root.character_mc.minAttack); //This is the ammount of damage done, it's a random amount worked out by the min attack taken away from the max attack, the random result is added to the min attack to get the final attack ammount. _root.enemyHealth_txt.text =; //This updates the enemy's health text _root.fightStat_txt.text += "You hit it\r"; _root.fightStat_txt.scroll += 1; } else { _root.fightStat_txt.text += "You missed it\r"; _root.fightStat_txt.scroll += 1; } } else { _root.fightStat_txt.text+="You killed it!\r"; _root.fightStat_txt.scroll += 1; _root.character_mc.xp += 20; _root.characterXP_txt.text = _root.character_mc.xp; _root.enemy_mc.gotoAndStop("dead"); clearInterval(_root.enemy_mc.attackInterval); //This stops the enemy attack sequence once the enemy is dead clearInterval(_root.character_mc.attackInterval);//This stops the character attack sequence once the enemy is dead } }, _root.character_mc.hitRate); //This is the last bit of the interval setup, it defines how often the interval should happen, in this case it's the character's hit rate // // // // // // // // // // //Enemy Attacking Character _root.fightStat_txt.text +="Enemy Initiating attack\r"; _root.fightStat_txt.scroll += 1; _root.character_mc.attackInterval = setInterval(function () { if (>0) { if (random(_root.character_mc.defence+_root.enemy_mc.attack)<=_root.enemy_mc.attack) { -=(random((_root.enemy_mc.maxAttack-_root.enemy_mc.minAttack)+1)+_root.enemy_mc.minAttack); _root.characterHealth_txt.text =; _root.fightStat_txt.text +="It hit you\r"; _root.fightStat_txt.scroll += 1; } else { _root.fightStat_txt.text += "It missed you\r"; _root.fightStat_txt.scroll += 1; } } else { _root.fightStat_txt.text += "It killed You!\r"; _root.fightStat_txt.scroll += 1; _root.character_mc.gotoAndStop("dead"); clearInterval(_root.character_mc.attackInterval); clearInterval(_root.enemy_mc.attackInterval); } }, _root.enemy_mc.hitRate); }; This is actually two lots of code, one is for the enemy, the other is for the character. Each section is one big interval with the events happening once the interval is up, in this case the interval length is the hitrate. Well I hope this has given you an idea of what sort of simple attack engine could be made and built on.
» Level Intermediate

Added: 2005-05-24
Rating: 4 Votes: 14
(10 being the highest)
» Author
Tutorial scripted and created by Oliver Sale
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!


  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: work2survive
» Title: Girl Walking 1kb
» Description: Walking girl, animation just takes 1kb, using high heels shoes, and fashion model attitude.
Featured Sound Loops
Image for equalize

Audio Player

» Author: BoxCat
» Title: CPU Talk
» Description: From the cyberpunk RPG, "Nameless: the Hackers" Completely Free to use! Keywords: edm hip hop rap electronic mysterious mission hacking chase exciting spy ios ambient chilling love happy sad angry suspense
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: nathan
» Description:

A recorded sound of the Dimensional Fork Gate.