Morgan Clarke Morgan Clarke - 4 months ago 24
CSS Question

Javascript Variable Not Updating Using Progress Bar

I am making a game and have used a progress bar to help increment some values on my html page. But, once the bar reaches 100% it updates the cost and the level but it only works once. After that it doesn't update the hard drive level no matter how many times you click it. How can I keep the variable updating?



//stats
var cash = document.getElementById('cash');
var ram = document.getElementById('ram');
var internet = document.getElementById('internet');
var hd = document.getElementById('harddrive');
var level = document.getElementById('level');
//shop
function levelup(t){
var bar = document.getElementById('lvelprogress');
bar.value = bar.value + t;
t = 0
if(t == 100){
bar.value = 100;
};
};
function upgradehd(al) {
var hdc = document.getElementById('hdc');
var hdl = document.getElementById('hdl');
var levelhd = 1;
var nhd = 250
var to = level * 20;
var nhdc = Math.floor(2.5 * hdc.innerHTML);
var bar = document.getElementById('progressBar');
var status = document.getElementById('status');
status.innerHTML = al+"%";
bar.value = al;
document.getElementById('lvelprogress').max = 100;
al++
var sim = setTimeout("upgradehd("+al+")",to);
if(al == 100){
var xp = hdc.innerHTML / 4;
levelup(xp);
status.innerHTML = "100%";
hdl.innerHTML = levelhd + 1;
hdc.innerHTML = nhdc;
hd.innerHTML = nhd + 50;
al=0;
status.innerHTML = al+"%";
bar.value = 0;
clearTimeout(sim);
};
};

.upgrade{
background-color: #66ff33;
width: 200px;
}
.shop{
background-color: black;
color: #66ff33;
width: 200px;
border: 5px solid #cccccc
}
.stats{
float: right;
height: 175px;
width: 150px;
background-color: #000;
color: #66ff33;
margin-left: 2px;
text-align: center;
display: inline-block;
}

<div class="stats">
<p>Level: <span id="level">1</span></p>
<progress style="width:150px;background-color:#66ff33;" id="lvelprogress" value="0" max="100"></progress>
<p>Cash: $<span id="cash">50</span></p>
<p>R.A.M: <span id="ram">100</span>MB</p>
<p>Internet Speed: <span id="internet">25</span>MB/s</p>
<P>Hard Drive: <span id="harddrive" >250</span>GB</P>
</div>
<div id="shop" class="shop">
<button class="exit" onclick="hideshop()">X</button>
<p id="uram">Upgrade R.A.M: $<span id="urc">50</span> Level:<span id="url"> 1</span><button style="border:2px solid white;color:#66ff33;background-color:#000;" onclick="upgradehd(0);">Upgrade</button></p>
<p id="uis">Upgrade Internet Speed: $<span id="isc">200</span> Level:<span id="isl"> 1</span><button style="border:2px solid white;color:#66ff33;background-color:#000;" onclick="upgradehd(0);">Upgrade</button></p>
<p id="uhd">Upgrade Hard Drive: $<span id="hdc">100</span> Level:<span id="hdl"> 1</span><button style="border:2px solid white;color:#66ff33;background-color:#000;" onclick="upgradehd(0);">Upgrade</button></p>
<progress class="upgrade" id="progressBar" value="0" max="100"></progress>
<span id="status"></span>
</div>




Answer

You always set the LevelValue to 1. var levelhd = 1; But you have to use the current Level from HTML or you save it into a var in your JavaScript.

I changed the line:
var levelhd = parseFloat(hdl.innerHTML);

//stats
var cash = document.getElementById('cash');
var ram = document.getElementById('ram');
var internet = document.getElementById('internet');
var hd = document.getElementById('harddrive');
var level = document.getElementById('level');
//shop
function levelup(t) {
  var bar = document.getElementById('lvelprogress');
  bar.value = bar.value + t;
  t = 0
  if (t == 100) {
    bar.value = 100;
  };
};

function upgradehd(al) {
  var hdc = document.getElementById('hdc');
  var hdl = document.getElementById('hdl');
  var levelhd = parseFloat(hdl.innerHTML);
  var nhd = 250
  var to = level * 20;
  var nhdc = Math.floor(2.5 * hdc.innerHTML);
  var bar = document.getElementById('progressBar');
  var status = document.getElementById('status');
  status.innerHTML = al + "%";
  bar.value = al;
  document.getElementById('lvelprogress').max = 100;
  al++
  var sim = setTimeout("upgradehd(" + al + ")", to);
  if (al == 100) {
    var xp = hdc.innerHTML / 4;
    levelup(xp);
    status.innerHTML = "100%";
    hdl.innerHTML = levelhd + 1;
    hdc.innerHTML = nhdc;
    hd.innerHTML = nhd + 50;
    al = 0;
    status.innerHTML = al + "%";
    bar.value = 0;
    clearTimeout(sim);
  };
};
.upgrade {
  background-color: #66ff33;
  width: 200px;
}
.shop {
  background-color: black;
  color: #66ff33;
  width: 200px;
  border: 5px solid #cccccc
}
.stats {
  float: right;
  height: 175px;
  width: 150px;
  background-color: #000;
  color: #66ff33;
  margin-left: 2px;
  text-align: center;
  display: inline-block;
}
<div class="stats">
  <p>Level: <span id="level">1</span>
  </p>
  <progress style="width:150px;background-color:#66ff33;" id="lvelprogress" value="0" max="100"></progress>
  <p>Cash: $<span id="cash">50</span>
  </p>
  <p>R.A.M: <span id="ram">100</span>MB</p>
  <p>Internet Speed: <span id="internet">25</span>MB/s</p>
  <P>Hard Drive: <span id="harddrive">250</span>GB</P>
</div>
<div id="shop" class="shop">
  <button class="exit" onclick="hideshop()">X</button>
  <p id="uram">Upgrade R.A.M: $<span id="urc">50</span> Level:<span id="url"> 1</span>
    <button style="border:2px solid white;color:#66ff33;background-color:#000;" onclick="upgradehd(0);">Upgrade</button>
  </p>
  <p id="uis">Upgrade Internet Speed: $<span id="isc">200</span> Level:<span id="isl"> 1</span>
    <button style="border:2px solid white;color:#66ff33;background-color:#000;" onclick="upgradehd(0);">Upgrade</button>
  </p>
  <p id="uhd">Upgrade Hard Drive: $<span id="hdc">100</span> Level:<span id="hdl"> 1</span>
    <button style="border:2px solid white;color:#66ff33;background-color:#000;" onclick="upgradehd(0);">Upgrade</button>
  </p>
  <progress class="upgrade" id="progressBar" value="0" max="100"></progress>
  <span id="status"></span>
</div>

Comments