尝试失败
在BABYLON.SceneLoader.ImportMesh…{newMeshes[0].position.x=10;}中有一些强制尝试,它使用本地项newMeshes[0]工作,但除此之外什么都不工作。
这是因为变量newMeshes
仅在回调函数中定义。如果您想获得函数之外的变量,则需要在全局范围内对其进行定义。要做到这一点,只需在调用ImportMesh
之前声明一个变量,并在ImportMesh
的回调函数内部将该变量设置为newMeshes[0]
,如下所示:
var meshisin = BABYLON.AbstractMesh;
// Define the variable in the global scope.
var skullMesh;
meshisin = BABYLON.SceneLoader.ImportMesh("", "scenes/", "skull.babylon", scene, function (newMeshes) {
skullMesh = newMeshes[0];
});
然后可以使用:skullMesh.position.x = 10;
更改网格的位置。
但是,由于加载网格需要1秒的时间,所以您可以延迟使用网格,直到它加载了setTimeout
,如下所示:
setTimeout(function() {
skullMesh.position.x = 10;
}, 1000);
总而言之,你的代码将变成:
var meshisin = BABYLON.AbstractMesh;
// Define the variable in the global scope.
var skullMesh;
meshisin = BABYLON.SceneLoader.ImportMesh("", "scenes/", "skull.babylon", scene, function (newMeshes) {
skullMesh = newMeshes[0];
});
setTimeout(function() {
skullMesh.position.x = 10;
}, 1000);
附言:在图片中发布代码通常不是一个好主意。