2017-02-06 22 views
-2

我用JavaScript寫了一個遊戲。它工作正常,但現在我試圖通過將代碼拆分爲更小的函數和文件來使代碼更加可重用且更易於調試.Bowow是播放功能,在遊戲循環中反覆呼叫: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> 

任何幫助將不勝感激。

+0

您在瀏覽器控制檯中遇到什麼錯誤? 「不起作用」並不能爲我們提供任何可用於診斷問題的信息。 **如何**不起作用? – Amy

+1

你已經回答了你自己的問題。如果將註釋標記之間的代碼放在函數外('play'),那麼該代碼將不再訪問該函數的局部變量('explorerHit')。您需要通過在更高範圍內聲明可以訪問函數外部所需的數據。 –

+0

瞭解更多關於作用域的信息,如果你想讓它被其他函數訪問,你的變量是局部於'play'的函數,使其成爲全局的 – Roljhon

回答

1

explorerHit變量在您的play()函數中聲明,所以它在該函數之外是不可見的。這被稱爲本地範圍。您需要將值作爲參數傳遞給checkForPlayerDamage()以便它可以有作爲:

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

而分裂出功能:

function checkForPlayerDamage(explorerHit) { ... 

(鑑於在checkForPlayerDamage()使用的所有其他變量是全球性的)

您可以在JavaScript上的不同的範圍力學刷在這裏:

What is the scope of variables in JavaScript?