变量不变,保持在0

时间:2014-05-23 01:26:45

标签: javascript html var

我试图制作增量型游戏。这个网站dmholley.co.uk提供了基本代码。

问题:购买游标后,游标数量不会改变。它只是保持在0。

这是index.html

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <link rel="stylesheet" type="text/css" href="interface.css" />
    </head>

<body>
    <button onclick="cookieClick(1)">Click Me!</button>
    <br />
    Cookies: <span id="cookies">0</span>
    <br />
    <button onclick="buyCursor()">Buy Cursor </button>
    <br />
    Cursors: <span id="cursors">0</span>
    Cursor Cost: <span id="cursorCost">10</span>
    <script type="text/javascript" src="main.js"></script>

</body>

继承main.js

var cookies = 0;
function cookieClick (number){
    cookies = cookies + number;
    document.getElementById("cookies").innerHTML = cookies;
};
var cursors = 0;
function buyCursor(){
    var cursorCost= Math.floor(10*Math.pow(1.1,cursors));
    if(cookies >= cursorCost){
        cursors = cursors + 1;
        cookies = cookies - cursorCost;
        document.getElementByID('cursors').innerHTML = cursors;
        document.getElementById('cookies').innerHTML = cookies;
    };
    var nextCost=Math.floor(10* Math.pow(1.1,cursors));
    document.getElementById('cursorCost').innerHTML = nextCost;
};
window.setInterval(function(){
    cookieClick(cursors);
}, 1000);

1 个答案:

答案 0 :(得分:0)

你必须点击“点击我”至少10次才能发生任何事情。

然后点击“购买光标”。

您会发现代码中有错误:

注意功能的情况:

document.getElementByID('cursors').innerHTML = cursors;
document.getElementById('cookies').innerHTML = cookies;

易于修复!