c# - Program acts as if variable doesn't exist, goes straight to "else" -


so, little problem ran here. whenever in combat, turn cycles once per second , no matter what, creature dies before player hits it. question is, how should "if" statement structured grab dynamic health total creature player fighting?

    public void startbattle()     {          messages.add(string.format("you engage in combat {0}", monster.name));         battletimer.start();     }      private void onbattleupdate(object sender, elapsedeventargs e)     {         if (monster.health <= 0)         {             actionresult result = monster.getattackresult();             messages.add(result.lookmessage);             state.character.causedamage(result.healthchange);             if (state.character.health <= 0)             {                 messages.add("you have died.");                 battletimer.stop();             }              int playerdamage = state.character.getdamage().healthchange;             messages.add(string.format("you swing weapon @ {0} , cause {1} damage", monster.name, playerdamage));             monster.causedamage(playerdamage);             messages.add(string.format("health : {0}/{1}", state.character.health, state.character.maxhealth));         }         else         {              int playerdamage = state.character.getdamage().healthchange;             messages.add(string.format("you swing weapon @ {0} , cause {1} damage", monster.name, playerdamage));             messages.add(string.format("the {0} died.", monster.name));             messages.add(string.format("you loot {0} gold {1}", monster.gold, monster.name));             messages.add(string.format("health : {0}/{1}", state.character.health, state.character.maxhealth));             state.character.gold += monster.gold;             mapmanager.removemonster(monster, mapmanager.mapname);             battletimer.stop();         }     } 

let me know if more info needed! writing first real program!!

edit: else works in combat, before added method make creature die, combat go on until player died.

monster defined in monster maker class as:

example rat:

public class monstermaker {     public static monster createrat()     {         monster monster = new monster();          monster.name = "rat";         monster.gold = 10;         monster.health = 10;         monster.lowdamage = 2;         monster.highdamage = 4;         monster.attackmessage1 = "{0} bites {1} damage.";         monster.attackmessage2 = "{0} slashes @ it's claws {1} damage.";         monster.attackmessage3 = "{0} rats out {1} damage.";         monster.missmessage1 = "{0} bites @ , misses.";         monster.missmessage2 = "{0} slashes @ , misses.";          return monster;     } 

these lines

if (monster.health <= 0)     {         actionresult result = monster.getattackresult(); 

seem saying "if monster has 0 or fewer hit points, have attack". meant > 0.

update

based on full code link in comments

i suspect core problem timer fires on , on while debugging, causing confusing behavior. modified beginning of handler return if event being processed, make easier step through code. not needed during normal execution, because timer interval vastly longer time process code.

    bool runningonbattleupdate = false;      public void onbattleupdate(object sender, elapsedeventargs e)     {         if (runningonbattleupdate) return;          try         {             runningonbattleupdate = true;             if (monster.isalive == false)             {                 // stuff             }             else             {                 // other stuff             }         }                 {             runningonbattleupdate = false;         }     } 

this property implemented incorrectly. throws stackoverflowexception because setter recursively calls itself.

public bool isalive {     { return _isalive; }     set     {         if (health <= 0)         {             _isalive = false;             isalive = false;          }         else         {             _isalive = true;             isalive = true;         }     } } 

you use

public bool isalive {     { return health > 0; } } 

you set state of isalive directly. however, doesn't make sense. instead, set current health , let isalive getter figure things out based on health.

and delete lines explicitly change isalive , _isalive like

monster.isalive = true; monster._isalive = true; 

you want prevent character getting in blow after dead

if (state.character.health <= 0) {     messages.add("you have died.");     battletimer.stop();     return; // don't want let dead character damage monster } 

note user interface project not set compile (right-click solution in solution explorer, select configuration manager, click build).

with these changes, able move south few times , defeat snake.