我用三个JS创建了墙。我尝试在点击事件上动态更改颜色。但它不起作用。以下代码中的错误是什么:
var materials = [ materialFront, materialSide ];
var material = new THREE.MeshFaceMaterial( materials );
if(path_type=="wall")
{
var mesh1 = new THREE.Mesh( geometry1, material );
object.add( mesh1 );
}
else {
var mesh2 = new THREE.Mesh( geometry1, material );
object.add( mesh2 );
}
object.rotation.x = Math.PI / 2;
object.position.y = parseInt(default_height*(floor_number-1));
scene.add( object );
Onclick更改颜色代码:
function color_change(color){
mesh1.material.color = new THREE.Color( color );
mesh1.material.needsUpdate = true;
}
我收到此错误:
mesh1未定义
答案 0 :(得分:0)
if(path_type=="wall")
{
var mesh1 = new THREE.Mesh( geometry1, material );
object.add( mesh1 );
}
变量mesh1
在if
范围内声明,无法从该范围外访问。您需要在全局范围内声明它以从其他函数访问它。像这样 -
var mesh1;
if(path_type=="wall")
{
mesh1 = new THREE.Mesh( geometry1, material );
object.add( mesh1 );
}