我開發這裏的增量遊戲停止值是鏈接的視覺參考:如何從進入負值
https://code.sololearn.com/WF65X6DEns7o/#css
我的問題是,按鈕可以點擊無限次,收入價值將進入負面。
如何禁用按鈕,如果玩家沒有足夠的收入來點擊按鈕
function buttonOne() {
a++;
document.getElementById("btnLabel1").innerHTML = " Units Owned : " + a;
income -= 500;
document.getElementById("HeaderLabel").innerHTML = "<b> OK, good, now let's watch as your money starts to generate slowly but surely. <p> After all no empire was built in a day. <p> When you have enough money you can buy more units. " ;
window.setInterval(function move() {
var elem = document.getElementById("myprogbar1");
var width = 1;
var id = setInterval(frame, 4);
function frame() {
if (width >= 100) {
clearInterval(id);
} else {
width++;
elem.style.width = width + '%';
}
}
}, 1000)
}
function buttonTwo() {
b++;
document.getElementById("btnLabel2").innerHTML = " Units Owned : " + b;
income -= 1000;
window.setInterval(function move() {
var elem = document.getElementById("myprogbar2");
var width = 1;
var id = setInterval(frame, 9);
function frame() {
if (width >= 100) {
clearInterval(id);
} else {
width++;
elem.style.width = width + '%';
}
}
}, 2000)
}
function buttonThree() {
c++;
document.getElementById("btnLabel3").innerHTML = " Lofts Owned : " + c;
income -= 2000;
window.setInterval(function move() {
var elem = document.getElementById("myprogbar3");
var width = 1;
var id = setInterval(frame, 19);
function frame() {
if (width >= 100) {
clearInterval(id);
} else {
width++;
elem.style.width = width + '%';
}
}
}, 3000)
}
<div id="gameMoneyBG">
<div id="gameMoney"> Income : $ 500 </div</div>
<button id="buttonOne" onclick="buttonOne()">
<b>Small Units</b></button>
<div id="progbar1">
<div id="myprogbar1"> </div>
</div>
<br /> <br />
<div id="btnLabel1"> Units Owned : 0 </div>
<div id="costLabel1">
Unit Cost : $ 500 </div>
<br /><br />
<button id="buttonTwo" onclick="buttonTwo()"><b>Large Units</b></button>
<div id="progbar2">
<div id="myprogbar2"> </div>
</div>
<br /><br />
<div id="btnLabel2"> Units Owned : 0 </div>
<div id="costLabel2"> Unit Cost : $ 1000 </div>
<br /><br />
<button id="buttonThree" onclick="buttonThree()"><b>City Lofts</b></button>
<div id="progbar3">
<div id="myprogbar3"> </div>
</div>
<br /><br />
<div id="btnLabel3"> Lofts Owned : 0 </div>
<div id="costLabel3"> Loft Cost : $ 2000 </div>
點擊後,檢查當前值,如果是低於零,則什麼也不做 – Swellar
歡迎SO。請下次使用'<>'創建一個[mcve],就像我爲你所做的那樣。不要忘記點擊TIDY之前保存 - 請修復代碼片段,所以它不會給出錯誤 - 你需要添加變量a,b,c,收入例如 – mplungjan
@mplungjan謝謝。我會記住下一個問題 –