2016-10-01 3 views
0

私はwebglアプリケーションを使用しています。ループの最初の行で定義されたstarsList[i].textureもののTypeError:xが未定義です - JS

TypeError: starsList[i] is undefined 
handleLoadedTexture(starsList[i].texture) 

:私はこのエラーを得た

function initTextures() { 

     for (i = 0; i < starsList.length; i++) { 
      starsList[i].texture = gl.createTexture(); 
      starsList[i].texture.image = new Image(); 
      starsList[i].texture.image.onload = function() { 
       handleLoadedTexture(starsList[i].texture) 
      } 
      starsList[i].texture.image.src = starsList[i].name + ".gif"; 
     } 
    } 

:私はこの機能を実行すると、私は、starsListアレイと、何らかの理由を持っています。
なぜですか?

答えて

0

あなたのオンロードコールバックがあなたのiと呼ばれるとき、それはstarsList.lengthに等しいです。

したがってstarsList[starsList.length]は未定義です。

コールバックを宣言するとき starsList[i].texture.image.onload = function() { handleLoadedTexture(starsList[i].texture) } starsListの特定の値はコピーされません。スコープ内のすべての変数が呼び出されたときにそれを見つけることを試みます。

各オンロードコールバックを特定の用途にバインドすることができますi

関連する問題