-
Simple RPG Combat System
Hi all. Just one quick question, if you were to create a really simple RPG combat system, where the player and the monsters have three stats (HP, Attack and Defense), and the following combat function would occur each time the player hit the "Fight!" button:
playerHP -= enemyAttack - playerDefense;
Same thing for the enemy. If playerHP <= 0, go to lose frame. If enemyHP <=, win.
How would you organize your information? I mean, if I want to add 100 monsters (besides adding their unique stats), how can I build an overall function to handle all the fighting system, so I don't have to build 100 additional functions like:
PHP Code:
btnFight.addEventListener(MouseEvent.CLICK, fightMonster1);
function fightMonster1(MouseEvent:Event):void
{
playerHP -= monster1Attack - playerDefense;
monster1HP -= playerAttack - monster1Defense;
txtPlayerHP.text = String("Player HP: "+ playerHP);
txtMonster1HP.text = String("Monster1 HP: "+ monster1HP);
}
This is for AS 3.0 by the way.
Thanks.
Oops, I placed the thread on the wrong forum. Sorry folks.
Last edited by Hurdarr; 06-02-2009 at 07:44 PM.
-
Use one var to change which monster you're fighting. For example:
Code:
function fightMonster1(monsterShell);
{
playerHP -= monster1Attack - playerDefense;
monster1HP -= playerAttack - monster1Defense;
txtPlayerHP.text = String("Player HP: "+ playerHP);
txtMonster1HP.text = String("Monster1 HP: "+ monster1HP);
}
Then just input the monster's name everytime you call the function.
-
Senior Member
You should make a Character class that holds the health information. That way, you can extend the main Character class into a Monster class. To make 100 monsters, just do new Monster() 100 times. After that, to do a battle system, have a function in the character class:
public function attack( attacker:Character):void
{
this.hp -= attacker.attack - this.defense;
}
Or make a global function that accepts 2 param's:
public function attack( attacker:Character, defender:Character):void
{
defender.hp -= attacker.attack - defender.defense;
}
WIP-ZOMBIES
I love vegetarians! More meat for the rest of us!
-
I have yet to fully understand the "this" property. What does it apply? The object of the function?
-
Senior Member
this applies to the class the this keyword is in. It's a way for an object to reference itself.
WIP-ZOMBIES
I love vegetarians! More meat for the rest of us!
-
this is just a variable. Whenever you call a function this is also declared automatically.
Every function has a scope(read more about variable and function scope) and whatever the scope of the function is that is what "this" will be equal to. For example. Lets say you declare a function "myFunc" in the first frame of your fla movie. the scope of the function is it's parent object, [Main Timeline] (i think) so if you draw a mental pic should look like this:
[Main Timeline]
|
+---myFunc
now if you type trace(this) inside of myFunc it should trace [Main Timeline];
All of it happens in the background, the as3 interpretor does it.
Here is another quick example.
var myObj = {
method_one: function(){trace(this)}
};
flash creates an object with 1 function property "method_one"
the scope of the function now is
[myObj]
|
+---method_one
Flash looks at the tree and creates a variable "this" inside method_one and makes it equal to the parent object, which in this case is myObj therefore this = myObj.
Keep in mind "this" is read only. Only flash as the rights to change it not the developer there for this is illegal in your code - var this = myObj.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|