其中一个按钮不适用于JS和HTML

时间:2015-10-27 12:09:38

标签: javascript html

所以我一直在编写一个闲置的游戏,我有一些按钮用于升级。之前我有3个,当我添加第四个时,由于某种原因它没有正常工作。

JavaScript的:

gainUpgrade: function() {
    if (engine.player.Energy >= engine.idleGainUpgrade) {
        engine.player.Energy -= engine.idleGainUpgrade;
        engine.idleGain += 0.5;
        engine.display.Energy.innerHTML = engine.player.Energy;
        engine.display.idleGain.innerHTML = engine.idleGain;
        engine.Status("Upgrade unlocked!");
        engine.idleGainUpgrade += (engine.idleGainPriceMultiplier * 50 * 2);
        engine.display.idleGainUpgrade.innerHTML = engine.idleGainUpgrade;
        engine.idleGainPriceMultiplier +=0.5;
    } else {
        engine.Status("Not enough energy!");
    }
},

这是与按钮相关联的功能,应该触发它的JavaScript是:

engine.clickables.gainUpgrade = document.getElementById("idlegainupgrade");
engine.clickables.gainUpgrade.addEventListener("click", function() {
    engine.gainUpgrade;
});

代码的HTML位:

<input type="button" id="idlegainupgrade" value="Increase gain from idle!"><br /><br />
<h6>
    Idle Gain: <span id="idlegain"></span>
    <br />Idle Gain Upgrade Cost: <span id="idlegainupgradecost"></span>
</h6>

我做错了什么?

2 个答案:

答案 0 :(得分:0)

替换:

engine.clickables.gainUpgrade.addEventListener("click", function() {
    engine.gainUpgrade;
});

使用:

engine.clickables.gainUpgrade.addEventListener("click", engine.gainUpgrade);

您没有在点击处理程序中调用gainUpgrade。将该函数作为对addEventListener的引用传递将起到作用。

答案 1 :(得分:-1)

engine.clickables.gainUpgrade = document.getElementById("idlegainupgrade");
engine.clickables.gainUpgrade.addEventListener("click", function() {
    engine.gainUpgrade();
});