三.js加载顺序 [英] Three.js loading order
问题描述
我的 Three.js Json-Loader 有问题.我有一些对象,它们的路径保存在一个数组中.现在我想加载它们并在列表中对它们进行排序,以便我可以选择它们.但是它们的加载顺序与加载后在我的数组中的顺序不同,因为它们的大小不同,所以小的在最前面,大的在最后.所以加载它们后我不知道对象的名称(名称是路径).
我的代码:
I have a problem with my Three.js Json-Loader. I have some object their path is saved in an array.
Now I want to load them and sort them in a List so that I can select them.
But the order they are loaded is a different than they are in my array after loading because they have different size so the small one are first and the bigger are last. So after loading them I don't know the name of the object ( the name is the path ).
My code :
for(var j=0;j<21;j++){
var path = objPath[j];
loader.load( path, function( geometry ) { save(geometry, path); } );
}
使用此代码,路径被提供给保存方法,但总是有最后一个 (objPath[20]) 路径,因为 for 循环比加载方法更快.我该怎么做才能使路径正确?
with this code the path is given to the save methode but there is always the last (objPath[20]) path because the for-loop is faster than the loading methode. What can I do that the path is the right?
推荐答案
您正被一个涉及 JavaScript 闭包规则的常见错误所困扰,请参阅 此链接.简而言之,要获得传递给 save 函数的正确路径,您需要将其包装到一个辅助函数工厂中,如下所示:(或如上链接所示)
You are being bitten by a rather common mistake involving JavaScript closure rules, see this link. In short, to get the correct path passed to the save function, you need to wrap it into a helper function factory, like this: (or as shown in the above link)
function saveHelper(path) {
return function(geometry) {
save(geometry, path);
}
}
for(var j=0;j<21;j++){
var path = objPath[j];
loader.load( path, saveHelper(path) );
}
这篇关于三.js加载顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!