我正在嘗試開發一款遊戲。JS數學造成總數爲負
但是我的數學是一個問題。每次總利潤值變爲負值。
我該如何解決?
var gameProfit = 10000;
var tinyOwned = 0;
var tinyCost = 10000;
var tinyIncome = 0;
function buyTiny(){
\t if (gameProfit >= tinyCost){
\t \t
\t \t tinyOwned += 1;
\t \t document.getElementById('tiny-owned').innerHTML = tinyOwned;
\t \t document.getElementById('current-profit').innerHTML = "Profit : $ " + (gameProfit -= tinyCost * tinyOwned);
\t \t document.getElementById('tiny-cost').innerHTML = "Next Cost : $ " + (tinyCost * tinyOwned + 10000);
\t \t document.getElementById('tiny-income').innerHTML = "Income : $ " + (tinyIncome = 15000 * tinyOwned); \t
\t }
}
var timer = 0;
var timeInterval = 10;
window.setInterval(function gameTimer() {
\t if (timer >= 0) {
\t \t timer++;
\t \t document.getElementById('timer-value').style.width = timer + "px";
\t }
\t if (timer >= 600) {
\t \t timer = 0 + 1;
\t \t document.getElementById('timer-value').style.width = "0px";
\t \t
\t \t tinyIncome = 15000 * tinyOwned;
\t \t gameProfit += tinyIncome;
\t \t
\t \t document.getElementById('current-profit').innerHTML = "Profit : $ " + (gameProfit);
\t \t
\t }
}, timeInterval);
任何人都可以看到在我的代碼的問題? gameProfit變量消極如何解決這個問題?
您可以提供工作示例而不是*錯誤製造商* – Justinas
請分享HTML代碼。 –
首先,如果您調用函數buyTiny並向您顯示我們的值: gameProfit(10 000)= gameProfit(10 000) - tinyCost(10000)* 1 = 0 所以在第一次調用gameProfit之後是0 但是我找不到你在哪裏調用這個函數 – Polux66