2015-06-20 65 views
1

我是新的three.js。基本上我想做一個像棋盤一樣的東西。我得到這個:錯誤設置顏色

for(x=-30; x<=50;x=x+10){ 
     for(y=-30; y<=20;y=y+10){ 
      var TileGeometry = new THREE.PlaneGeometry(5,5); 
      var TileMaterial = new THREE.MeshBasicMaterial(); 
      var Tile = new THREE.Mesh(TileGeometry,TileMaterial); 
      //tile.receiveShadow = true 
      Tile.rotation.x=-0.5*Math.PI; 
      Tile.position.x = x; 
      Tile.position.y = 0; 
      Tile.position.z = y; 
      Tile.castShadow=true; 
      scene.add(Tile); 
      //whiteTile.material.color.setHex(0x000000); 
      tiles.push(Tile); 
     } 
    } 


    for(var tile in tiles){ 
     if(count%2==0){ 
      tile.material.color.setHex(0x000000); 
      } 
     else{ 
      tile.material.color.setHex(0xffffff); 
      } 
     count++; 
    } 

瓷磚正確定位。問題是設置顏色時。 我得到:

Uncaught TypeError: Cannot read property 'color' of undefined

+0

你也可以使用着色器http://blog.2pha.com/demos/threejs/shaders/checker.html – 2pha

回答

0

for ... in循環迭代的對象的所以tile實際上是索引不陣列內的對象時,使用正常for環或forEach

for (var i = 0; i < tiles.length; i++) 
    tiles[i].material.color.setHex(i % 2 ? 0xffffff : 0); 

Read more about for ... in loops on MDN