2017-02-06 8 views
-2

JavaScriptでゲームを書いたことがありますが、今は小さな関数とファイルに分割することで、コードの再利用とデバッグが簡単になりました.Belowは、再生機能で、それを繰り返しゲームループ内で呼び出される:サブ関数に分割したときに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; 
      } 

     } 


     } 

このコードは動作します。しかし、コードのセクション(ハッシュタグから作られた行の中にあるセクション)を別の関数の中に入れ、別のファイルに格納してから、このファイル内でその関数を呼び出すと、次のようになりますエラー:

 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()関数内で宣言されているため、その関数の外側には表示されません。これはローカルスコープと呼ばれます。

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

そして、分割さアウト機能:それはそこにも利用できるようにするには、checkForPlayerDamage()にパラメータとして値を渡す必要があり

function checkForPlayerDamage(explorerHit) { ... 

(他のすべての変数がcheckForPlayerDamage()で使用していることを考えるとグローバルである)

あなたが現在JavaScriptで異なるスコープ力学をブラッシュアップすることができます。

What is the scope of variables in JavaScript?

関連する問題