Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2006
    Posts
    21
    Thanks
    1
    Thanked 1 Time in 1 Post

    unending loop problem

    Hi. I was wondering if anyone could help me with a small problem in a script I wrote. (http://digitonblaze.110mb.com/) I learned Javascript a few years back, and so I decided to refresh my memory (not really having used any heavy javasript since then) by making a small rpg project completely in JavaScript over the past few days. Technically, everything in here is pretty much beginners stuff, it's just a little long. The thing seems to be working without a hitch for the most part, but I first started noticing a bug on the fight sequences a while back. At first I thought it was a simply typo mistake. I fixed it, and the bug seemed to dissapear. But then a bit later it started showing up again, this time much further ahead then it had been previously. The code for the fight sequence was basically this (there's some other stuff for getting bonus points before the actual fight code, but the bug existed prior to its addition, so the problem has nothing to do with it):
    Code:
    function fight(enemy, level, turn, ehp, eattack, edefence, epower, espell1, espell2, espell3, espell4, eisspell) {
    if (hp<=0) {
    	notify("You don't have any HP! Revive yourself before trying to fight.", wellisit);
    	gotomaincity();
    }
    if (hp>0) {
    	if (enemy=="goblin"&&level==1) {
    		if (turn==1) {
    		if (eattack>defence) {
    			notify("The goblin attacked you with its small dagger!", wellisit);
    			if (armour>0&&endurance>armourtake) {
    				hp -= eattack - defence - armouradd;
    				endurance -= armourtake;
    				notify("It hit your " + armourname + "!", wellisit);
    				notify("You lost " + armourtake + " endurance points! You now have " + endurance + ".", wellisit);
    			}
    			if (armour==0||endurance<armourtake) {
    				hp -= eattack - defence;
    			}
    			if (hp<0) {
    				hp = 0;
    			}
    			notify("You now have " + hp + " HP left.", wellisit);
    			notify("The goblin has " + ehp + " HP left.", wellisit);
    			gototraining();
    			if (hp<=0) {
    				hp = 0;
    				notify("The goblin defeated you! Your hp is now on 0! You will have to find some way to revive your hp.", wellisit);
    				gotomaincity();
    			}
    			if (hp>0&&ehp>0) {
    				fight("goblin", 1, 2, ehp, 3, 0, 0, 0, 0, 0, 0, 0);
    			}
    		}
    		if (eattack<=defence) {
    			notify("The goblin attacked but you stepped quickly out of the way and blocked it!", wellisit);
    			gototraining();
    			if (hp>0&&ehp>0) {
    				fight("goblin", 1, 2, ehp, 3, 0, 0, 0, 0, 0, 0, 0);
    			}
    		}
    		}
    		if (turn==2) {
    		if (confirm("Do you want to attack normally? Choose no to select a spell to use.")==false) {
    			if (spell1==0&&spell2==0&&spell3==0&&spell4==0) {
    				notify("You don't know any spells!", wellisit)
    				if (hp>0&&ehp>0) {
    					fight("goblin", 1, 2, ehp, 3, 0, 0, 0, 0, 0, 0, 0);
    				}
    			}
    		}
    		else {
    			notify("You attacked the goblin!", wellisit);
    			if (attack>edefence) {
    				if (weapon>0&&strength>=weapontake) {
    					ehp -= attack + weaponadd - edefence;
    					strength -= weapontake;
    					notify("You attacked with " + weaponname + "!", wellisit);
    					notify("You lost " + weapontake + " strength! You now have " + strength + " strength.", wellisit);
    				}
    				if (weapon==0||strength<weapontake) {
    					ehp -= attack - edefence;
    				}
    				notify("You now have " + hp + " HP left.", wellisit);
    				if (ehp<0) {
    					ehp = 0;
    				}
    				notify("The goblin has " + ehp + " HP left.", wellisit);
    				gototraining();
    				if (ehp<=0) {
    					howmuchexp = 50;
    					howmuchexp += Math.round(Math.random() * 100);
    					howmuchexp -= Math.round(Math.random() * 100);
    					if (howmuchexp<0) {
    						howmuchexp = 0;
    					}
    					exp += howmuchexp;
    					gold += 100;
    					notify("You defeated the goblin! You gained " + howmuchexp + " exp. You have " + hp + " HP left. You gained 100 gold. You now have " + gold + " gold.", wellisit);
    					if (exp>=maxexp) {
    						levelup();
    					}
    					gototraining();
    				}
    				if (ehp>0&&hp>0) {
    					fight("goblin", 1, 1, ehp, 3, 0, 0, 0, 0, 0, 0, 0);
    				}
    			}
    			if (attack<=edefence) {
    				notify("You attack the goblin, but it's no use! The goblin easily blocks it!", wellisit);
    				gototraining();
    				fight("goblin", 1, 1, ehp, 3, 0, 0, 0, 0, 0, 0, 0);
    			}
    		}
    	}
    	}
    }
    The code is trimmed down a little, and I only included one of the monsters. The problem is strange, although simple: If you're on level 2 (I recently discovered it also occurs on some other levels but I'm not sure which ones), then the fight will loop again after you finish it. It just says that you won and then starts over again until either you reach another level or you run out of HP. I don't normaly get stuck by something as simple as a broken loop, but I have looked at this thing over and over and it's driving me crazy! So, if anyone could help me I'd really appreciate it. Thanks.

  • #2
    New Coder
    Join Date
    Jun 2006
    Posts
    21
    Thanks
    1
    Thanked 1 Time in 1 Post
    P.S.:
    Something's really wierd about this bug. Sometimes it shows up and sometimes it doesn't. That makes it really hard to track (and is partially why I was unable to solve it).

  • #3
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    Without even looking it at (because it's a bit long) I would recommend running this in FireFox and watching your javascript console. It will point you towards your errors.

    Now I'll look at your code... (jeopardy music)

    Nope, no idea. I'd have to play with it to give you better advice, but no time for that. Try the console thing, try alerting whenever you call the fight method all the necessary stats, etc.

    Try cleaning up your code, your conditionals make everything hard to follow. Easy to follow code is easy to debug.

  • #4
    New Coder
    Join Date
    Jun 2006
    Posts
    21
    Thanks
    1
    Thanked 1 Time in 1 Post
    Yes, thank you, but unfortunately I've already tried both of those things before asking for help. Along with a lot of other things including changing a lot of variable names (not easy), searching the entire thing meticulously for even one single typo, restructuring the entire sequence, adding more conditions to clarify when events are supposed to trigger, run it through more than one debugger, it really just isn't working. Nothing I do can seem to find anything wrong with the code and every time I think I must have been imagining the thing, it starts showing up again. I guess it is a little too much for someone with so much to do. Thanks anyway.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •