我正在做一个闲置/答题器游戏,当我尝试购买手臂时,它什么也没做。我已经尝试了几乎所有已知的知识,但无法通过搜索Internet获得任何结果。

HTML:

<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <link rel="stylesheet" type="text/css" href="css/interface.css" />
        <title>Jujhar Clicker</title>
    </head>
    <body>
        <script type="text/javascript" src="js/main.js"></script>
        <div style="position: absolute; top: 30px; right: 660px;"><button id="Face" onClick="faceClick(1)" style="position: static" align="center"><img src="images/face.png"><p align="center" style="line-height:-200em">Click Me!</p></button></div>
        <div style="position: absolute; top: 10px; right: 690px;"><a>Faces: </a><span id="faces" style="right:20px">0</span></div>
        <button id="RoboticArm" onClick="buyArm" style="position: static" align="center"><img src="images/ArmStore.png" /></button>
        <div style="position: absolute; left: 20px;"><span id="arms">Amount: 0</span>
        <a>Cost: </a>
        <span id="armCost">10</span></div>
    </body>
</html>


Javascript:

var faces = 0;
var arms = 0;

function faceClick(number) {
    faces = faces + number;
    document.getElementById("faces").innerHTML = faces;
};

function buyArm() {
    var armCost = Math.floor(10 * Math.pow(1.1,arms));
    if (faces >= armCost) {
        arms = arms + 1;
        faces = faces - armCost;
        document.getElementById('arms').innerHTML = "Amount: " + arms;
        document.getElementById('faces').innerHTML = faces;
    };
    var nextCost = Math.floor(10 * Math.pw(1.1,arms));
    document.getElementById('armCost').innerHTML = nextCost;
};

最佳答案

看来您只有一个小错误。您没有正确调用buyArm()函数,因为您忘记添加括号。

onClick="buyArm"更改为onClick="buyArm()"

Demo

测试此类错误的一种好方法是在Chrome中打开它,右键单击>“检查元素”,然后在“开发人员工具”屏幕底部读取所有错误。

08-19 14:51