简体   繁体   English

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

[英]How to fix 'ReferenceError: hello is not defined' when it is

I'm trying to code a game and I want to make it so that when you click a button, it increases the number. 我正在尝试编写游戏代码,所以我想制作它,以便在您单击按钮时增加它的数量。 My game is like a mining game and you click to get ores and at the top right is a box which tells you what you are mining and you can see what you are mining, except when I click the mine button, it comes with the error which says ReferenceError: hello is not defined. 我的游戏就像一个采矿游戏,您单击以获取矿石,在右上角是一个告诉您正在开采什么并可以看到正在开采什么的框,但当我单击“采矿”按钮时,它带有错误上面显示ReferenceError: hello is not defined. The function hello is the function which gives you the ores. 函数hello是为您提供矿石的函数。

I have tried fixing up some other functions which give you helpers in exchange for help but it didn't change anything, also I checked on stack overflow, but there wasn't anything that could help me. 我尝试修复一些其他功能,这些功能可以为您提供帮助者以换取帮助,但它什么都没有改变,我还检查了堆栈溢出,但是没有任何可以帮助我的东西。 (Keep in mind I am 10 years old) HTML: (请记住,我今年10岁)HTML:

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

JavaScript: 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;
        }


    }

Sorry, but the functions needed are quite long. 抱歉,所需的功能很长。 If you want to see the whole code, go to megagames.me/games/mining.html 如果您想查看整个代码,请访问megagames.me/games/mining.html

I expected the out put of hello() to increment some of the ores, but it gave ReferenceError: hello is not defined. 我期望hello()会增加一些矿石,但它给ReferenceError: hello is not defined.

Make sure your JavaScript is linked in the head tag or above the button. 确保您的JavaScript链接在head标签中或按钮上方。 Otherwise you'll be calling a function that doesn't exist yet. 否则,您将调用一个尚不存在的函数。 An easy way to remember is to think of it as a book, you read from top to bottom the same way JavaScript executes top to bottom. 一种容易记住的方法是将其视为一本书,从上至下阅读,就像JavaScript从上至下执行一样。

Also, try using Let and Const instead of Var and try using Switch Cases instead of if else all over. 另外,请尝试使用Let和Const代替Var,并尝试使用Switch Cases而不是其他方法。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未被捕获的ReferenceError:您好未定义 - Uncaught ReferenceError: hello is not defined 如何修复&#39;ReferenceError:未定义matrixo&#39; - How to fix ' ReferenceError: matrixo is not defined' 如何修复:ReferenceError: validateEmail 未定义 - How to fix: ReferenceError: validateEmail is not defined 导入类时如何修复“未捕获的ReferenceError:XXX未定义” - How to fix 'Uncaught ReferenceError: XXX is not defined' when importing a class 如何修复ReferenceError:当所有SDK都位于HTML文件中时,未定义firebase - How to fix ReferenceError: firebase is not defined when all SDKs are in the HTML file 如何修复“ReferenceError:index is not defined”问题 - How to fix “ReferenceError: index is not defined” problem 如何修复 vuejs 中的“ReferenceError: require is not defined” - How to fix "ReferenceError: require is not defined" in vuejs 如何修复未捕获的 ReferenceError:熔岩未定义 - How to fix Uncaught ReferenceError: lava is not defined 如何修复未捕获的 ReferenceError:未定义要求 - How to fix Uncaught ReferenceError: require is not defined 初学者:如何修复JavaScript中的“ ReferenceError myFunc未定义” - Beginner: How to fix 'ReferenceError myFunc is not defined' in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM