Jump to content

Timeout problem


Fire Dragon

Recommended Posts

I planned battle system for my text based game,and it works quite well.However,I got problem with timing.I set timing like w3schools' example shows,but it won't stop my system.I can't figure why :( Before I post code,here is some information,how code works,and what I want it do.Well,player must raise "power level" using button "Raise power" button.When player haves enought power,he/she can attack using "attack" button,what takes same value HP from enemy than power's value is.Enemy always waits 5 seconds,and attacks.Attack may cause 10 or 20 HP.If you lose your HP,code directes you to Google :) What is problem then?Well,if enemy's HP is 0 or under,I want it stops timing what cycles function what attacks to player.However,I can't get it work.After HP is under 0,function continues attacking.I used same kind code what is in W3schools' example.And finally,here is my code:

<html><head><script>var player_HP=100;var power=0;var enemyHP=100;/*****************************ENEMY ATTACK********************************/function enemyattack(){var enemyattack=Math.floor(Math.random()*10);var time if (enemyHP==0 || enemyHP<0) { clearTimeout(time) alert("You win!") } if (enemyattack<5&&enemyattack>1) {   player_HP=player_HP-10;   document.getElementById('player_HP').value=player_HP; }   else if (player_HP==0 || player_HP==-10 || player_HP==-20){  window.location="http://www.google.com"  document.getElementById('player_HP').value=player_HP;} else {   player_HP=player_HP-20;   document.getElementById('player_HP').value=player_HP; } time=setTimeout("enemyattack()",5000) }/******************************PLAYER ATTACK***************************/function attack(){enemyHP = enemyHP - power;document.getElementById('attack').value=enemyHP;power = power - power;document.getElementById('powerlevel').value=power;}function powerup(){power=power+1document.getElementById('powerlevel').value=power;}</script><body><input type="button" value="Start!" onclick="enemyattack()" /><p>HP <input type="input" value="100" id="player_HP" /></p><p>Enemy HP <input type="input" value="100" id="attack" /></p><p>Power level <input type="input" value="0" id="powerlevel" /></p><input type="button" value="Attack!" onclick="attack()" /><input type="button" value="Raise power" onclick="powerup()" /></body></html>

Thank you very much,if you can help me with this problem.It makes me crazy :)

Link to comment
Share on other sites

Theres quite a few little things you can improve in your code. - Theres alot of shortcuts in javascript you could do with using, e.g. player_HP -= 10; IS THE SAME AS player_HP = player_HP - 10; power++; IS THE SAME AS power = power + 1;- As well as setTimeout(), there is also a setInterval() function, which is more suitable for what you are doing here. This was part of the problem with your function not stopping. setInterval calls a function every x ms.- Instead of doing (enemyHP == 0 || enemy < 0) you can just use (enemy <= 0), "less than or equal to"I fixed up your code a little, so you can see it below. You'll also notice I fixed a few other little problems and theres still some that could be improved, I'll leave it up to you to find them.

<html><head><script>var player_HP=100;var power=0;var enemyHP=100;var enemyTime;function startGame(){	enemyTime = setInterval("enemyattack()",5000);}/*****************************ENEMY ATTACK********************************/function enemyattack(){	if (enemyHP<=0){  alert("You win!");  clearInterval(enemyTime)	}else{	  var enemyattack = Math.floor(Math.random()*10);  if (enemyattack<5 && enemyattack>1){  	player_HP -= 10;  	document.getElementById('player_HP').value=player_HP;  }else{  	player_HP -= 20;  	document.getElementById('player_HP').value=player_HP;  }    if (player_HP < 1){  	window.location="http://www.google.com"  }	}}/******************************PLAYER ATTACK***************************/function attack(){	enemyHP -= power;		document.getElementById('attack').value=enemyHP;		power = 0;	document.getElementById('powerlevel').value=power;}function powerup(){	power++;	document.getElementById('powerlevel').value=power;}</script><body>	<input type="button" value="Start!" onclick="startGame()" />		<p>HP <input type="text" value="100" id="player_HP" /></p>		<p>Enemy HP <input type="text" value="100" id="attack" /></p>		<p>Power level <input type="text" value="0" id="powerlevel" /></p>		<input type="button" value="Attack!" onclick="attack()" />		<input type="button" value="Raise power" onclick="powerup()" /></body></html>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...