2015-12-19 222 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

相关问题