for循环只取第一个数组值

时间:2016-06-02 16:53:47

标签: javascript arrays three.js

我试着像下面的代码一样调用数组值,但是当onclicked我能够成功获得数组中的第一个素材而不是其他素材时,由于某种原因它会停止循环。请让我知道我哪里出错了。

var White = new THREE.MeshPhongMaterial( {
            color:0xf6daa5,
        //  specular: 0x000000,
            roughness: 0.7,
            shininess: 0,
        //  bumpMap: mapHeight,
            bumpScale: 12,
            emissive: null,
            emissiveIntensity : null,
            reflectivity: 0.1
   } );
var sink = new THREE.MeshPhongMaterial( {
     color: 0x224466,
     combine: THREE.MixOperation,
     reflectivity: 0.8,
     side: THREE.DoubleSide,
     shininess: 40,
     reflectivity:0

     opacity:null
   } )
var gold = new THREE.MeshPhongMaterial( {
     color: 0x529dc3,                  
     specular: 0x508fbb,            
     shininess: 60,
     emissive:0x070606,
     reflectivity:0.7,
     opacity:0.8,
     shading: THREE.FlatShading,
     combine: THREE.MultiplyOperation
   } )  // have declared these globally

EventsControls.attachEvent('onclick', function() {
    var colors = [White, sink, gold];
    for (var i = 0; i < colors.length; i++) {
        console.log(colors.length);
        object.traverse(function(child) {
            if (child instanceof THREE.Mesh) {

                if (child.material.name == "w__") {

                    child.material = colors[i];
                }
            }
        })
    }
});

2 个答案:

答案 0 :(得分:0)

数组索引从0开始,你从1开始。

&#13;
&#13;
// filler to make the code work
var THREE = {
  "MeshPhongMaterial": function() {},
  "MixOperation": null,
  "DoubleSide": null,
  "FlatShading": null,
  "MultiplyOperation": null
};

var White = new THREE.MeshPhongMaterial({
  color: 0xf6daa5,
  //  specular: 0x000000,
  roughness: 0.7,
  shininess: 0,
  //  bumpMap: mapHeight,
  bumpScale: 12,
  emissive: null,
  emissiveIntensity: null,
  reflectivity: 0.1
});


var sink = new THREE.MeshPhongMaterial({
  color: 0x224466,
  combine: THREE.MixOperation,
  reflectivity: 0.8,
  side: THREE.DoubleSide,
  shininess: 40,
  reflectivity: 0,
  opacity: null
});

var gold = new THREE.MeshPhongMaterial({
  color: 0x529dc3,
  specular: 0x508fbb,
  shininess: 60,
  emissive: 0x070606,
  reflectivity: 0.7,
  opacity: 0.8,
  shading: THREE.FlatShading,
  combine: THREE.MultiplyOperation
}); // have declared these globally


// commented out parts that aren't relevant to the issue
//EventsControls.attachEvent('onclick', function() {
var colors = [White, sink, gold];
for (var i = 0; i < colors.length; i++) {
  console.log(i + " = " + colors.length);
  //  object.traverse(function(child) {
  //    if (child instanceof THREE.Mesh) {
  //
  //      if (child.material.name == "w__") {
  //
  //        child.material = colors[i];
  //      }
  //    }
  //  })
}
//});
&#13;
&#13;
&#13;

object定义在哪里?否则,它会在for循环中出错。

答案 1 :(得分:-1)

您的数组由多个字符串组成,每个字符串必须放在双引号EventsControls.attachEvent('onclick', function() { var colors = ["White", "sink", "gold"]; for (var i = 1; i < colors.length; i++) { console.log(colors.length); object.traverse(function(child) { if (child instanceof THREE.Mesh) { if (child.material.name == "w__") { child.material = colors[i]; } } }) } });

之间
xts

另请参阅:http://www.w3schools.com/js/js_arrays.asp