【问题标题】:for loop taking only the first array valuefor 循环只取第一个数组值
【发布时间】:2016-06-03 01:40:34
【问题描述】:

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

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];
                }
            }
        })
    }
});

【问题讨论】:

  • colors数组中的值在哪里定义?
  • 看起来您需要在颜色名称周围加上引号。 'White', 'sink', 'gold' (sink?) 或在某处定义这些颜色名称变量。
  • 数组索引从 0 开始,你从 1 开始。
  • 也尝试过使用单引号,但没有成功,我已经在全局范围内定义了这些变量 White、sink 和 gold。
  • IMTheNachoMan:我可以在点击时访问第一个数组对象“White”,尝试使用 0(即 i=0),并尝试使用 i=1 检查颜色“sink”。这也工作正常。问题是它没有循环。

标签: javascript arrays three.js


【解决方案1】:

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

// 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];
  //      }
  //    }
  //  })
}
//});

object 是否在任何地方定义?否则会在for循环中出错。

【讨论】:

  • 是的,我已经定义了对象,并且正在使用对象子对象来更改其材质。我想知道这些更改是否可以反映一个数组元素,为什么不能反映其他两个,我相信我在循环它时犯了一个错误。
【解决方案2】:

你的数组由几个字符串组成,每个字符串都必须放在双引号"之间

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];
                }
            }
        })
    }
});

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

【讨论】:

  • Tijmen,我尝试使用双引号,但那个 onclick 对象消失了。之后。但是,当我启动不带引号的数组时,它设法调用第一个数组对象 White。
  • @Tijmen - 我认为您没有看到白色、水槽和金色实际上是对象。将它们放在字符串中是行不通的。
  • 我刚刚在问题中更新了全局定义的变量,有没有其他循环方式我可以尝试这种情况。一个while循环就够了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-06-24
  • 2015-04-08
  • 1970-01-01
  • 2011-12-14
  • 2019-07-07
  • 1970-01-01
  • 2012-09-03
相关资源
最近更新 更多