c# - Weird division by zero error -
well, im trying calculate slope of line, , naturally, when point becomes undefined when try divide zero. handle in try catch , still getting division 0 error, in rather... unexpected spot...
here code:
private void tmrenemymovement_tick(object sender, eventargs e) { (int = 0; < enemies.count; i++) { int enex = enemies[i].getenemyworldposx(); int eney = enemies[i].getenemyworldposy(); int playerx = player.getplayerworldposx(); int playery = player.getplayerworldposy(); double slope = -100000000; try { slope = (getdistancebetween(eney, playery)) / (getdistancebetween(enex, playerx)); } catch (dividebyzeroexception) {//division 0 exception handled here. slope = getdistancebetween(eney, playery) / 1; } int multiplicative = 1; int rise = 1;//convert.toint32(slope * multiplicative); int run = multiplicative; text = convert.tosingle(slope) + ""; if (enemies[i].getenemyworldposx() < player.getplayerworldposx() && enemies[i].getenemyworldposy() > player.getplayerworldposy()) {//enemy resides in quadrant 2 enemies[i].moveenemyto(run, rise, "+-"); } else if (enemies[i].getenemyworldposx() < player.getplayerworldposx() && enemies[i].getenemyworldposy() < player.getplayerworldposy()) {//enemy resides in quadrant 3 enemies[i].moveenemyto(run, rise, "+-"); } else if (enemies[i].getenemyworldposx() > player.getplayerworldposx() && enemies[i].getenemyworldposy() > player.getplayerworldposy()) {//enemy resides in quadrant 1 enemies[i].moveenemyto(run, rise, "-+"); } else if (enemies[i].getenemyworldposx() > player.getplayerworldposx() && enemies[i].getenemyworldposy() < player.getplayerworldposy()) {//enemy resides in quadrant 4 enemies[i].moveenemyto(run, rise, "--"); } } bitmap bmp = map.getmap() bitmap; using (graphics drawenemy = graphics.fromimage(bmp)) { (int = 0; < enemies.count; i++) { drawenemy.drawimage(enemies[i].getsprite(), new pointf(enemies[i].getenemyworldposx(), enemies[i].getenemyworldposy())); } } }
and strange reason, in weird spot doesnt me:
this method defined follows:
private int worldpositiony = 0; public int getenemyworldposy() { return worldpositiony; }
as can see, simple getter, why getting division 0 error #1 @ all, when handled in try catch, , #2 on line? have missed something?
try recompile program , ensure in debug mode. not use try/catch calculating slope - satement long. use
var distancex = getdistancebetween(enex, playerx); if (distancex != 0) slope = getdistancebetween(eney, playery) / distancex; else slope = getdistancebetween(eney, playery);
and exception caught not critical. can press continue , go on. or can switch off handled exceptions (uncheck user-handled in debug->exceptions->common language runtime). or can run program without attached debugger (use ctrl+f5).
Comments
Post a Comment