Reply to topic  [ 3 posts ] 
 Adding RPG elements to your game (AS2) 
Author Message
Level 38
Level 38
User avatar

Cash on hand:
435.45

Bank:
2,750,364.30
Posts: 10364
Joined: Sun Oct 26, 2008 5:47 am
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
Image
Spoiler: show
OLD VERSION, BITCHES!
Image


Mon Nov 19, 2012 10:27 am
Profile E-mail
ℱᒪ૪ᓰﬡᘐ ᖘ⋒ᖇᖰᒪᙓ ᖘᙓﬡᓮᔕ
ℱᒪ૪ᓰﬡᘐ ᖘ⋒ᖇᖰᒪᙓ ᖘᙓﬡᓮᔕ
User avatar

Cash on hand:
258,935,827.73

Bank:
7,777,777.77
Posts: 19745
Joined: Fri Mar 04, 2011 9:57 pm
Location: ЇИ УОЦЯ MЇЙD FЦCКЇЙG ЇT ЇЙTО ОBLЇVЇОЙ
Group: Їи$aиїту
Country: Nepal (np)
Post 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

_________________
ImageImage
Image
?
Їи$aиїту Group! | Ultimate Fh Tribute!
© 2010 -2099 Odin Anarkis. All Rights Reserved.

Quotes
Spoiler: show
Image
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.

_________________
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
2 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.


Mon Nov 19, 2012 6:32 pm
Profile E-mail WWW
Level 1
Level 1
User avatar

Cash on hand:
646,302.11

Bank:
1,569,148.01
Posts: 114
Joined: Mon Jan 02, 2012 4:45 pm
Group: 1991
Post Re: Adding RPG elements to your game (AS2)
These are nice :)

_________________
Suicide Alice



Spoiler: show
11/29/2012
Image



ImageImage

ImageImage

Image

_________________
Click the icon to see the image in fullscreen mode  
4 pcs.
Click the icon to see the image in fullscreen mode  
100 pcs.
Click the icon to see the image in fullscreen mode  
1 pcs.
Click the icon to see the image in fullscreen mode  
36 pcs.


Tue Nov 20, 2012 9:46 am
Profile E-mail
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 
 

Similar topics

 
3-Words Game
Forum: ./General Spam
Author: Jewsus
Replies: 35
Video game music
Forum: ./Music
Author: Topkek
Replies: 8
LETS PLAY A GAME :)
Forum: ./General Spam
Author: Aragorn
Replies: 17
Game Uploaded
Forum: ./General Spam
Author: Clockwork47
Replies: 6
Top


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

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Mods Database :: Imprint :: Crawler Feeds :: Reset blocks
Designed by STSoftware for PTF.

Portal XL 5.0 ~ Premod 0.3 phpBB SEO