增量游戏错误

时间:2014-12-10 12:01:40

标签: javascript html5

我正在制作增量游戏,但是当我加载游戏时,它会增加更多小数并计入“无限”。 我做了一个codepen,所以你可以自己看到错误 尝试并单击图片几次,然后按保存并加载。 为什么快速望远镜的数量会这样上升?

The Codepen

    var faze = 0;
function fazeClick(number){
    faze = faze + number;
    document.getElementById('faze').innerHTML = faze;
}
var mntDew = 0;
function buyDew(){
    var mntDewCost = Math.floor(10 * Math.pow(1.1,mntDew));
    if(faze >= mntDewCost){
        mntDew = mntDew +1;
        faze = faze - mntDewCost;
        document.getElementById('mntDew').innerHTML = mntDew;
        document.getElementById('faze').innerHTML = faze;
    };
    var nextCost = Math.floor(10 * Math.pow(1.2,mntDew));
    document.getElementById('mntDewCost').innerHTML = nextCost;
};
function save(){
    localStorage.setItem('faze', JSON.stringify(faze));
    localStorage.setItem('mntDew', JSON.stringify(mntDew));
    //add more here
};
function load(){
    if (localStorage.getItem('mntDew')){
        faze = localStorage.getItem('faze');
    };
    if (localStorage.getItem('faze')){
        faze = localStorage.getItem('faze');
    };
    //add more here
};
function deleteSave(){
    localStorage.removeItem("save");
};

window.setInterval(function(){
    fazeClick(mntDew);
}, 1000);


// <button type="button" onClick="buyDew()">Buy Mountain Dew</button> #buyCursor
//Mountain Dew: <span id="mntDew">0</span><br/> #cursors
//cost: <span id="mntDewCost">10</span> #cursorCost

1 个答案:

答案 0 :(得分:0)

您的'错误'的责任在于以下说明:

window.setInterval(function(){
    fazeClick(mntDew);
}, 1000);

你的计数器开始递增,因为点击'购买山露'按钮增加了'mntDew var'的值,之前设定的间隔调用fazeClick(0);

我真的很抱歉,但我无法理解你想要达到的目标