分成子函数时JavaScript不起作用

时间:2017-02-06 18:00:02

标签: javascript

我用JavaScript编写了一个游戏。它运行了,但是现在我试图通过将它分成更小的函数和文件来使我的代码更可重用且更容易调试。下面是播放函数,即在游戏循环中反复调用:

   function play(deltaTime) {

        if (gameScene.visible == true) {

            explorer.x += explorer.vx * deltaTime;
            explorer.y += explorer.vy * deltaTime;

            //Contain the explorer inside the area of the dungeon
            contain(explorer, {
                x: 1,
                y: 1,
                width: canvasWidth,
                height: canvasHeight
            });

            var explorerHit = false;

            makeEnemiesMove();

            //##############################################################################

            //If the explorer is hit...
            if (explorerHit) {

                if (!damageSound.playing()) {
                    damageSound.play();

                }

                //Make the explorer semi-transparent
                explorer.alpha = 0.5;

                //Reduce the width of the health bar's inner rectangle by 1 pixel
                healthBar.outer.width -= 1;
            } else {
                //Make the explorer fully opaque (non-transparent) if it hasn't been hit
                explorer.alpha = 1;
            }

           //################################################################

            //Does the explorer have enough health? If the width of the `innerBar`
            //is less than zero, end the game and display "You lost!"
            if (healthBar.outer.width < 0) {

                gameOverSound.play();
            }
            //Check for a collision between the explorer and the treasure
            if (hitTestRectangle(explorer, treasure)) {
                //If the treasure is touching the explorer, center it over the explorer
                treasure.x = explorer.x + 8;
                treasure.y = explorer.y + 8;

             if (carrying < 1) { 

                    pickUpSound.play();

                    carrying = 1;
                }

            }

            //If the explorer has brought the treasure to the exit,
            //end the game and display "You won!"
            if (hitTestRectangle(treasure, door)) {

                victorySound.play();

                state = end;
            }

        }


      }

此代码有效。但是当我尝试将一段代码(位于由hashtags构成的行内的部分)放在一个单独的函数中,存储在一个单独的文件中,然后继续在该文件中调用该函数时,我得到以下内容错误:

     Uncaught ReferenceError: explorerHit is not defined

The function I made to run this bit of code looks like this:

function checkForPlayerDamage() {
    //If the explorer is hit...
        if (explorerHit) {

            if (!damageSound.playing()) {
                damageSound.play();

            }


            //Make the explorer semi-transparent
            explorer.alpha = 0.5;
            //Reduce the width of the health bar's inner rectangle by 1 pixel
            healthBar.outer.width -= 1;
        } else {
            //Make the explorer fully opaque (non-transparent) if it hasn't been hit
            explorer.alpha = 1;
        }

    }

我试图在原始文件中调用它,如下所示:

checkForPlayerDamage();

错误消息中引用的explorerHitVariable是在调用此函数之前定义的,如下所示:

索引文件中引用了相关文件,如下所示:

    var explorerHit = false;

    makeEnemiesMove();

    checkForPlayerDamage();

相关的JavaScript文件在索引文件中引用,如下所示:

<script language="JavaScript" type="text/javascript" src="gameScene/checkForPlayerDamage.js"></script>

<script language="JavaScript" type="text/javascript" language="JavaScript" type="text/javascript" src="play.js"></script>

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

explorerHit变量在play()函数中声明,因此在该函数外部不可见。这称为局部范围。您需要将值作为参数传递给checkForPlayerDamage(),以便它也可以在那里使用:

...
makeEnemiesMove();
checkForPlayerDamage(explorerHit);
...

分裂出来的功能:

function checkForPlayerDamage(explorerHit) { ...

(鉴于checkForPlayerDamage()中使用的所有其他变量都是全局的。)

您可以在这里了解JavaScript中不同的范围机制:

What is the scope of variables in JavaScript?