2015-12-19 221 views
-1

我似乎無法在我的代碼中使currentCost [0]或buildingA增加。爲什麼我不能更改變量?

我的問題可能是在這裏:

{ 
    document.getElementById("costA").innerHTML = currentCost[0] 
    document.getElementById("amountA").innerHTML = buildingA 
    document.getElementById("canPay").innerHTML = "You just bought another!" 
    count.postMessage({action:'subtract', amount:currentCost[0], once:true}); 
    currentCost[0] = Math.ceiling(nlogn(currentCost[0]+1)) + 5 
    buildingA = buildingA + 1 
} 

這是一個if語句中。

我的目標是當建築物被購買時,它會增加一個存在的類型的建築物的數量,並根據預定的功能,即newPrice = oldPrice*log10(oldPrice)增加價格。

但是,當我購買建築A時,價格將保持在15,並且它總是說我有0 buildingA's。

+0

你應該在這裏發佈你的源代碼,而不是鏈接到你的整個存儲庫。這裏的代碼發佈應該是[MCVEs](http://stackoverflow.com/help/mcve) – ray

回答

0

好的,在你的代碼中有一條線var nlogn = nlogn();這使得nlogn是一個變量,所以它給出了TypeError: nlogn is not a function。 更改變量名稱。

並把腳本放在<body>的末尾。

完整代碼。

<!DOCTYPE html> 
<head> 
<title>Test incremental</title> 

</head> 

<body> 
Your money: <output id="result"></output><br><br> 

<button onclick="buyA();">Buy Building A</button><br> 
Cost of next one: <output id="costA"></output><br> 
<output id="canPay"></output><br> 
You own <output id="amountA"></output> Building A's! 

<script language="JavaScript"> 

var buyCost = 0; 
var buyable = true; 
var count; 
var buildingModifier = 0; 
var cost = 0; 
var currentCost = [15, 25, 50, 100] 
var money = 0; 
//var nlogn = nlogn(); 

var buildingA = 0; 

function beginGame() { 
    if(typeof(Worker) !== "undefined") { 
     if(typeof(count) == "undefined") { 
      count = new Worker("counterWorker.js"); 
     } 
     count.onmessage = function(event) { 
      document.getElementById("result").innerHTML = event.data; 
      money = event.data 
     }; 
    } else { 
     document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Workers..."; 
    } 
} 

function nlogn(n) { 
    return n * Math.log10(n); 
} 

function checkBuyable() { 
    if(cost < money) { 
     buyable = true; 
    } else { 
     buyable = false; 
    } 
} 

var costA = document.createElement("costA") 
var canPay = document.createElement("canPay") 
var amountA = document.createElement("amountA") 

function buyA() { 
    cost = currentCost[0]; 
    checkBuyable(); 
    if (buyable == true) { 
     document.getElementById("costA").innerHTML = currentCost[0] 
     document.getElementById("amountA").innerHTML = buildingA 
     document.getElementById("canPay").innerHTML = "You just bought another!" 
     count.postMessage({action:'subtract', amount:currentCost[0], once:true}); 
     currentCost[0] = Math.ceil(nlogn(currentCost[0]+1)) + 5 
     buildingA = buildingA + 1 
     setTimeout(resetCanPay, 1000) 
    } else { 
     document.getElementById("canPay").innerHTML = "Too Expensive to buy another!" 
     setTimeout(resetCanPay, 1000) 
    } 
    document.getElementById("costA").innerHTML = currentCost[0] 
} 

function resetCanPay() { 
    document.getElementById("canPay").innerHTML = " " 
} 

currentCost[0] = Math.ceil(nlogn(currentCost[0]+1)) 
document.getElementById("costA").innerHTML = currentCost[0] 

beginGame(); 

</script> 

</body> 
+0

嗯,這仍然不起作用。我仍然可以購買15樓,它不會讓我等到20多。 – Nachtara

+0

嗨,我更新了答案。請檢查。 – snvrthn

+0

我已經在整個代碼中聲明瞭nlogn。這不是問題。編輯:哦,那是問題所在。我認爲它可以告訴一個變量和一個函數。 – Nachtara

相關問題