我的代码中似乎无法使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栋建筑物。
答案 0 :(得分: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>