法力回复升级设定间隔

时间:2019-05-24 02:42:00

标签: javascript html

当我按下Mp regen技能树按钮后,它应该从技能点中减去1,然后在Manapoints上加1。然后,我需要存储它,然后运行一个设置间隔的东西,但是我无法使其正常工作。我的功能似乎还可以,但是我无法复制我在Python中所做的工作。

http://prntscr.com/nsm5yy

我不确定如何解决此问题;我真的是每隔一段时间就新来的。我确实尝试过:

window.setInterval(function () {
    ManaPointsregen(Mpregen);
}, 1000);

这是我的Manaregen的JavaScript函数(这是我第二次尝试使用其他方法,但仍然无法正常工作):

var rebirths = 0;
var HitPoints = 0;
var MaxHitpoints = 10;
var ManaPoints = 0;
var MaxManapoints = 10;
var SkillPoints = 0;

function buyRebirth() {
    var rebirthCost = Math.floor(10 * Math.pow(1.1, rebirths));
    if (cookies >= rebirthCost && rebirths < 1){
        cookies = cookies - rebirthCost
        HitPoints = HitPoints + 10;
        ManaPoints = ManaPoints + 10;
        SkillPoints = SkillPoints + 1;
        rebirths = rebirths + 1;
        document.getElementById("rebirths").innerHTML = rebirths;
        document.getElementById('cookies').innerHTML = cookies;
        document.getElementById("HitPoints").innerHTML = HitPoints;
        document.getElementById("ManaPoints").innerHTML = ManaPoints;
        document.getElementById("rebirthCost").innerHTML = rebirthCost;
        document.getElementById("SkillPoints").innerHTML = SkillPoints;
    }
    if (cookies >= rebirthCost)
    if(rebirths >= 1){
        cookies = cookies - rebirthCost
        rebirths = rebirths + 1;
        SkillPoints = SkillPoints +1;
        document.getElementById('cookies').innerHTML = cookies;
        document.getElementById("rebirthCost").innerHTML = rebirthCost;
        document.getElementById("rebirths").innerHTML = rebirths;
        document.getElementById("SkillPoints").innerHTML = SkillPoints;

    }
    var nextCost5 = Math.floor(10 * Math.pow(1.1, rebirths));
    document.getElementById('rebirthCost').innerHTML = nextCost5;

    }




var Mpregen = 0;

function Manaregen(){
    if (ManaPoints < MaxManapoints && rebirths >- 1 && MaxManapoints >=0){
        ManaPoints = ManaPoints +1;
        set.interval( Manaregen, 1000);
    }
    document.getElementById("ManaPoints").innerHTML = ManaPoints;

}

function ManaPointsregenbuy(){
    if (SkillPoints > 0 && rebirths >=1)
        SkillPoints = SkillPoints - 1;
        Mpregen = Mpregen +1;
        set.interval(Manaregen, 1000);

    document.getElementById("SkillPoints").innerHTML = SkillPoints;
    document.getElementById("Mpregen").innerHTML = Mpregen;
}

JavaScript Setinverval:

var cookies = 0;

function cookieClick() {
    cookies = cookies + Strength;
    document.getElementById("cookies").innerHTML = cookies;
    checkCursor()
}
var UP_INTERVAL = 1000; 
setInterval(cookieClick, UP_INTERVAL);

html按钮代码:(我尚未将按钮ID用于任何东西)

<button id="Mpreg" onclick="ManaPointsregenbuy">Mp regen</button>
                <br />

我希望从技能树中购买MP回复后:

http://prntscr.com/nsmdmd

使我的法力每1000毫秒增加1(模拟法力回复),直到达到我的最大法力。但是按钮的实际输出没有任何作用。我不知道如何设置一个有效的setinterval函数来做到这一点。

1 个答案:

答案 0 :(得分:2)

onclick="ManaPointsregenbuy"

应该是

onclick="ManaPointsregenbuy()"

然后您有几行像

set.interval(.....)

设置时间间隔的正确语法如下:

var myInterval = setInterval(callbackFunction, 1000);

这将设置一个间隔,可以用

取消
clearInterval(myInterval)

您可以在https://www.w3schools.com/jsref/met_win_setinterval.asp

中找到更多信息。