Jump to content

Displaying updated variables


joeyoung2509

Recommended Posts

Afternoon all, I'm writing a text based game in an effort to learn JavaScript, my current issue is with the user interface, I have displayed in both top corners of the screen the HitPoint values of the user and which ever mob you are currently battling, my issue is that the values do not update after the player or mob take a hit.

 

for example this is the code used to determine how much is hit;

if (input.indexOf("attack") > -1) {				if (input == "attack"){												 var userDmg = Math.floor(Math.random() * 100);				 var mobDmg = Math.floor(Math.random() * 80);									$("#message_attack").html("You attack for: " + userDmg + " ---- Goblin HP left " + (mobHP -= userDmg)).clone().insertBefore("#placeholder").hide().fadeIn(3000);					$("#message_attack").html("The Goblin attacks back for: " + mobDmg + " ---- Your HP left " + (userHP -= mobDmg)).delay(2000).insertBefore("#placeholder").hide().fadeIn(3000);													}		}

This is how I am displaying the HTML

<div id="mobhealthbar" class="auto-style3" style="width: 180px; position: absolute; right: 20px; top: 50px; height: 25px;"></div><script type="text/javascript">document.getElementById("mobhealthbar").innerHTML= "MOB HP: " + mobHP;  </script><div id="userHealthBar" class="auto-style2" style="width: 180px; left: 21px; position: absolute; top: 60px; height: 25px;"><script type="text/javascript">document.getElementById("userHealthBar").innerHTML= "USER HP: " + userHP;  </script></div>

any advice would be welcome

 

Link to comment
Share on other sites

You need to put these lines inside the function that updates the values:

document.getElementById("mobhealthbar").innerHTML= "MOB HP: " + mobHP;document.getElementById("userHealthBar").innerHTML= "USER HP: " + userHP;

If you have them right in the HTML document they'll only execute once.

  • Like 1
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...