|
|
|
Page 1 of 1
|
[ 3 posts ] |
|
Adding RPG elements to your game (AS2)
Author |
Message |
Parpol
Level 38
Posts: 10364 |
Joined: Sun Oct 26, 2008 5:47 am |
Cash on hand: 435.45
Bank: 2,750,364.30
Group: Dev Team |
|
Adding RPG elements to your game (AS2)
First off. Learn to use functions. If you have not learned functions, then you do not deserve this.
This thread is open for suggestion. I will teach you any RPG element you ask for. Because this subject is so big, I will add more RPG element as time goes.
Health and Attack The Logic
IN THE FIRST FRAME (initialization): Note that I have added comments in the code.
Code: //the random number generator function getRandomNumber(min:Number, max:Number):Number { var difference:Number = max - min; //calculate difference return Math.floor(Math.random()*( difference+1 ) ) + min; //return random number between min and max } //Example: //var randomNumber:Number = getRandomNum(3,7); //randomNumber will now be either 3,4,5,6 or 7.
var health:Number; //create a number variable called health. var maxHealth:Number; //create a maximum health variable.
var baseDamage:Number; //The lowest possible damage your attack can make. (if defense is added, it could be lower) var maxDamage:Number; //The highest possible damage you could make. (The final damage will be somewhere between these two, and determined by chance.)
health = maxHealth = 100; //both health and maxHealth are now 100. baseDamage = 10; maxDamage = 15; //damage goes betweeen 10 and 15
var enemyHealth:Number; //enemy health var enemyMaxHealth:Number; //max health for enemy. var enemyBaseDamage:Number; //the lowest possible damage the enemy attack could make var enemyMaxDamage:Number; //the highest possible damage the enemy could make.
Also, this example only works when fighting one enemy at a time. If you want more enemies, move the enemy initialization code to the same place as where you determine the enemy stats and be sure to call it correctly outside it's object (for example if the enemy vars are in the movieclip foulBeast, then you call it by _root.foulBeast.health;) You also need to give the enemy movieclip an instance name "foulBeast" if you call _root.foulBeast. any name will do as long as they both match. If you're only going to fight one enemy at a time, it allocates less memory if you use global variables in the first frame instead of in every enemy. (Some just allow flash to allocate 5mb memory which is pretty low.)
IN THE ENEMY'S INITIALIZATION CODE: (when creating the enemy, or the frame it is created in)
Code: enemyHealth enemyMaxHealth = 50; //this particular enemy shall have 50 health. enemyBaseDamage = 5; //minimum damage is 5 enemyMaxDamage = 8; //maximum damage is 8
WHEN DEALING DAMAGE: (usually on the frame you hit your enemy)
Code: enemyHealth -= getRandomNum(baseDamage, maxDamage); //substracts from the enemy health a random value between your base attack and max attack damage. if(enemyHealth <= 0) { enemyDies(); //replace with whatever you want. perhaps enemy.gotoAndPlay("die"); }
TAKING DAMAGE:
pretty much the same.
Code: health -= getRandomNum(enemyBaseDamage, enemyMaxDamage); if(health <= 0) { die(); } The Visualization
Now you want an HP bar or something, right? Well, there are two ways. One which allows your complete customization for each life, and one that is 100% accurate as long as your resolution allows it. (zooming in will make it look even more accurate.)
Our first approach is actually very much like loading bars except that the variable determining your bar is your health in relation to maxHealth instead of bytes loaded in relation to total bytes.
1: Make a movieclip. Some kind of bar or whatever the fuck turns you on.
2: Give the movieclip an instance name (like "healthBar" or something.)
3: Give it X amount of frames (or keyframes.) Each frame represents a certain percentage of your health, meaning if you make 100 frames, each frame shows your health at 1% for each. In other words, frame 55 is your health bar at 55%. Give it 10 frames and the accuracy of your visual health bar is reduced to 10% per each frame. Make sure you count the amount of frames you use, preferably saving it as a variable healthBarFrames:Number;
4: Now let's calculate which frame we're supposed to be on.
Code: currentHealthFrame = int(health/(maxHealth/healthBarFrames));
5: and now simply call healthBar.gotoAndStop(currentHealthFrame); from wherever you want to update your health bar. replace "healthBar" with whatever instance name you chose for your health bar and you're done.
The second approach requires a little knowledge in transformation, but don't worry. I'll lead you through it all. It is also limited to a certain degree.
1: Create your health bar movieclip and give it an instance name. (let's call it healthBar like the other approach.)
2: Go inside it and move everything so that the origin is at one of the sides. (this side will be the point where you have 0 health left.)
3: exit again and create the following variables on your first frame in root, (or wherever you want to initialize the health bar. You need to have the health bar on stage at the time the code is called.):
Code: var healthWidth:Number; var originalHealthWidth:Number;
4: we're going to use the width of the bar as an indicator on how much life you have left. For that we need to get the original width of the movieclip.
Code: originalHealthWidth = healthBar._width; //get healthbar movieclip's width
5: Now we need to calculate the width based on it's original width, how much health you've got and what your max is. Since that's all that is left, we'll apply the calculation to the width directly and be done.
Code: healthBar._width = health/(maxHealth/originalHealthWidth); //calculate the percentage of your width
And there you go. Two approaches on how to make a health bar. LOGIC & VISUALIZATION COMPLETE!
Questions? Suggestions? Requests?
_________________ My Pixiv
- Spoiler: show
- OLD VERSION, BITCHES!
|
Mon Nov 19, 2012 10:27 am |
|
|
Odin Anarki
ℱᒪ૪ᓰﬡᘐ ᖘ⋒ᖇᖰᒪᙓ ᖘᙓﬡᓮᔕ
Posts: 19749 |
Joined: Fri Mar 04, 2011 9:57 pm |
Cash on hand: 258,935,944.23
Bank: 7,777,777.77
Location: ЇИ УОЦЯ MЇЙD FЦCКЇЙG ЇT ЇЙTО ОBLЇVЇОЙ |
Group: Їи$aиїту |
Country: |
|
Re: Adding RPG elements to your game (AS2)
this is oh so perfect of a tutorial that i can visualize doing it in flash as i read, combined with your functions tutorial this makes for some pretty clean code
gonna show these lovelys to alice
_________________
?Їи$aиїту Group! | Ultimate Fh Tribute!
© 2010 -2099 Odin Anarkis. All Rights Reserved. Quotes - Spoiler: show
who149 wrote: I'm trying i'm trying~ i'm making I'll try too slowly up my posting. At least once a day for a bit. Then I'll up that too twice, then four, then 8 and so on. Until eventually I wake up one morning and find out that I am actually an Idiot hero. On some quest too cheat on his gf or raise affection of 5 women who conveniently live in my the same dorm as me. In which I only have 100 days to seduce them all.
Remon wrote: Now we can dominate the porn industry, camera industry, AND the world! YomToxic wrote: YOU BETTER STAY ALIVE OR ELSE I WILL HUNT YOU DOWN AND RAPE YOU DEAD.
|
Mon Nov 19, 2012 6:32 pm |
|
|
Suicide Alice
Level 1
Posts: 114 |
Joined: Mon Jan 02, 2012 4:45 pm |
Cash on hand: 646,302.11
Bank: 1,569,148.01
Group: 1991 |
|
Re: Adding RPG elements to your game (AS2)
These are nice
_________________
Suicide Alice
|
Tue Nov 20, 2012 9:46 am |
|
|
|
|
Page 1 of 1
|
[ 3 posts ] |
|
Who is online |
Users browsing this forum: No registered users and 4 guests |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum
|
|
|
|