A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Combat End Conditionals

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

    Combat End Conditionals

    Hi all,

    I'm trying to code a very simple combat system between a player and a monster. Each one has three variables: HP, Attack and Defense.

    The formula for attack is really simple: monsterHP - monsterDefense. The player also gets attacked when I press the "Fight" button.

    So heres the deal:

    If the player beats the monster (monsterHP <= 0 and playerHP >= 0) I should go to the Win frame.

    If the monster beats the player (playerHP <= 0 and monsterHP >= 0), I should be directed to the Lose frame.

    If both of the fighters have HP <= 0 I should go to the DRAW frame. This is what I'm having trouble with it.


    PHP Code:
    stop();

    var 
    playerHP:int 1;
    var 
    playerAttack:int 100;
    var 
    playerDefense:int 5;

    var 
    monsterHP:int 1;
    var 
    monsterAttack:int 10;
    var 
    monsterDefense:int 4;

    btnFight.addEventListener(MouseEvent.CLICKfightMonster); 

    function 
    fightMonster(MouseEvent:Event):void 

        
    playerHP -= monsterAttack playerDefense
        
    monsterHP -= playerAttack monsterDefense
         
        
    txtPlayerHP.text String("Player HP: "playerHP); 
        
    txtMonsterHP.text String("Monster1 HP: "monsterHP); 
        
        if(
    playerHP <= 0)
        {
            
    playerHP == 0;
            
    trace("Player loses!");
        }
        else if(
    monsterHP <= 0)
        {
            
    monsterHP == 0;
            
    trace("You beat the Generic Weak Monster!");
        }
        else if(
    monsterHP <= && playerHP <= 0)
        {
            
    trace("Draw!");
        }
         

    The first two conditionals work fine but I'm having a hard time with the Draw statement.

    Also, is this a good way to code or should I use switches or something else?

    Thanks.

  2. #2
    Senior Member
    Join Date
    May 2009
    Posts
    138
    You need to have your draw condition first because both the first conditions will be true when your draw condition is true.

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