A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Simple RPG Combat System

  1. #1
    Senior Member
    Join Date
    Jan 2008
    Posts
    107

    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.CLICKfightMonster1);

    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.

  2. #2
    Member
    Join Date
    Apr 2009
    Posts
    43
    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.

  3. #3
    Senior Member Pazil's Avatar
    Join Date
    Sep 2006
    Location
    Ontario, Canada
    Posts
    913
    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!

  4. #4
    Senior Member
    Join Date
    Jan 2008
    Posts
    107
    I have yet to fully understand the "this" property. What does it apply? The object of the function?

  5. #5
    Senior Member Pazil's Avatar
    Join Date
    Sep 2006
    Location
    Ontario, Canada
    Posts
    913
    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!

  6. #6
    Senior Member
    Join Date
    Feb 2004
    Posts
    312
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center