如何修复“ ReferenceError:hello未定义”

时间:2019-09-08 02:41:31

标签: javascript html

我正在尝试编写游戏代码,所以我想制作它,以便在您单击按钮时增加它的数量。我的游戏就像一个采矿游戏,您单击以获取矿石,在右上角是一个告诉您正在开采什么并可以看到正在开采什么的框,但当我单击“采矿”按钮时,它附带了错误其中表示ReferenceError: hello is not defined.函数hello是为您提供矿石的函数。

我尝试修复一些其他功能,这些功能可以为您提供帮助者以换取帮助,但它没有改变任何东西,我也检查了堆栈溢出,但是没有任何可以帮助我的东西。 (请记住我今年10岁) HTML:

<div class="mwrapper">
    <button id="minebutton" onclick="hello()">Mine!</button>
    </div>

JavaScript:

//defining the vars.
    var stonei = 0;
    var deepness = 0;
    var stone = 0; 
    var silveri = 0;
    var silver = 0;
    var goldi = 0;
    var gold = 0;
    var platinumi = 0;
    var platinum = 0;
    var diamondi = 0;
    var diamond = 0;
    var alexandritei = 0;
    var alexandrite = 0;
    var amethysti = 0;
    var amethyst = 0;
    var unobtaniumi = 0;
    var unobtanium = 0;
    var emeraldi = 0;
    var emerald = 0;
    var tubi = 0;
    var tub = 0;
    var blockN;
    var block = 0;
    var money = 0;
    var stoneSold = 0;
    var silverSold = 0;
    var goldSold = 0;
    var clickers = 0;
    var moneyEver = 0;
    var BpS = 0;
//defining element to shorten code later
    var blockEl = document.getElementById("block");
    var btxtEL = document.getElementById("btxt");
    var moneyEl = document.getElementById("money"); 
    //changing what the 'Block you are mining' says
    var findBlock = function(b) {
        if (b === 0) {
            blockEl.style.backgroundColor = "grey";
            btxt.innerHTML = "Stone";
            blockN = "stone";
        }
        else if (b === 1) {
            blockEl.style.backgroundColor = "silver";
            btxt.innerHTML = "Silver";
            blockN = "silver";
        }
        else if (b === 2) {
            blockEl.style.backgroundColor = "gold";
            btxt.innerHTML = "Gold";
            blockN = "gold";
        }
        else if (b === 3) {
            blockEl.style.backgroundColor = "rgb(90, 89, 89)";
            btxt.innerHTML = "Platinum"
            blockN = "platinum";
        }
        else if (b === 4) {
            blockEl.style.backgroundColor = "rgb(185, 242, 255)";
            btxt.innerHTML = "Diamond"
            blockN = "diamond";
        }
        else if (b <= 10) {
            btxt.innerHTML = "Not coded yet";
        }
        //hehe
        else {
            btxt.innerHTML = "WHAAAA?????????";
        }
    }
    //adds materials to the left sidebar
    var createBlock = function(b) {
        if (b === 0) {
            stonei += 1;
            stone += 1;
            document.getElementById("stonei").innerHTML = stonei;
            document.getElementById("stone").innerHTML = stone;

        }
        else if (b === 1) {
            silveri += 1;
            silver += 1;
            document.getElementById("silveri").innerHTML = silveri;
            document.getElementById("silver").innerHTML = silver;
        }
        else if (b === 2) {
            goldi += 1;
            gold += 1;
           document.getElementById("goldi").innerHTML = goldi;
            document.getElementById("gold").innerHTML = gold;
        }
        else if (b === 3) {
            platinumi += 1;
            platinum += 1;
            document.getElementById("platinumi").innerHTML = platinumi;
            document.getElementById("platinum").innerHTML = platinum;

        }
        else if (b === 4) {
            diamondi += 1;
            diamond += 1;
            document.getElementById("diamondi").innerHTML = diamondi;
            document.getElementById("diamond").innerHTML = diamond;

        }
        //not coded rest

    }
    //From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
    //for finding the block when you mine
    function getRandomInt(min, max) {
        min = Math.ceil(min);
        max = Math.floor(max);
        return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
    }
    //when you click the mine button it does this
    var hello = function() { 

        if (deepness === stone + silver + gold + platinum + diamond && stone >= stonei && silver >= silveri && gold >= goldi && platinum >= platinumi && diamond >= diamondi && stoneSold == stone - stonei &&  moneyEver == money + clickers &&typeof hsjsahjkd === 'undefined' || hsjsahjkd === null) {
        if (deepness <= 50) {
            block = 0;
            findBlock(block);
            deepness += 1;
            createBlock(block)
        }
        else if (deepness >= 50, deepness < 150) {
            block = getRandomInt(0, 2);

            findBlock(block);

            deepness += 1;
            createBlock(block)

        }
        else if (deepness >= 150, deepness < 250) {
            block = getRandomInt(0, 3);

            findBlock(block);

            deepness += 1;
            createBlock(block)

        }
        else if (deepness >= 250, deepness < 350) {
            block = getRandomInt(0, 4);

            findBlock(block);

            deepness += 1;
            createBlock(block)

        }
        else if (deepness >= 350) {
            block = getRandomInt(0, 5);

            findBlock(block);

            deepness += 1;
            createBlock(block)

        }

        }
        else {
            btxt.innerHTML = "Cheater.";
             stonei = 0;
             deepness = 0;
             stone = 0; 
             silveri = 0;
             silver = 0;
             goldi = 0;
             gold = 0;
             platinumi = 0;
             platinum = 0;
             diamondi = 0;
             diamond = 0;
             alexandritei = 0;
             alexandrite = 0;
             amethysti = 0;
             amethyst = 0;
             unobtaniumi = 0;
             unobtanium = 0;
             emeraldi = 0;
             emerald = 0;
             tubi = 0;
             tub = 0;
             stoneSold = 0;
             silverSold = 0;
             goldSold = 0;
             clickers = 0;
             moneyEver = 0;
             BpS = 0;
             console.log("You cheated. The game restarted.")
             if (typeof hsjsahjkd == 'undefined') {
                 var hsjsahjkd = 1;
             }
             else {
                 hsjsahjkd += 1;
             }


             document.getElementById("cheat").innerHTML = hsjsahjkd;
        }


    }

对不起,但是所需的功能很长。如果您想查看整个代码,请转到megagames.me/games/mining.html

我希望hello()的产量增加一些矿石,但它给了ReferenceError: hello is not defined.

1 个答案:

答案 0 :(得分:0)

确保您的JavaScript链接在head标签中或按钮上方。否则,您将调用一个尚不存在的函数。一种容易记住的方法是将其视为一本书,从上至下阅读,就像JavaScript从上至下执行一样。

此外,请尝试使用Let和Const代替Var,并尝试使用Switch Cases而不是其他方法。 :-)