Thanks for your reply. I modified my code following your instruction. Now it can print the value of arr0[1].
However, when I want to use arr0 to be the position of two sphere objects and show them on screen. The spheres will at the position (0, 0, 0). It seems that I still do not pass values to these arrays.
My code now looks likes:
var sceneScreen;
var arr0 = new Array(2);
var arr1 = new Array(2);
var arr2 = new Array(2);
var arr3 = new Array(2);
for (var i = 0; i < 2; i++) {
arr0[i] = new THREE.Vector3();
arr1[i] = 0.5;
arr2[i] = 0.5;
arr3[i] = new THREE.Vector3();
}
function show(){
console.log('arr0[1] ' + arr0[1].x + ' ' +arr0[1].y+ ' ' + arr0[1].z + ' ');
}
function init()
{
// ......
new THREE.FileLoader().load('data.txt',
function (data) {
var tmp = data.split('\n');
for (var i = 0; i < 16; i++)
{
var t0 = tmp[i * 4];
var t1 = tmp[i * 4+1];
var t2 = tmp[i * 4+2];
var t3 = tmp[i * 4+3];
var ttt0 = t0.split(' ').map(parseFloat);
arr0[i] = new THREE.Vector3(ttt0[0],ttt0[1],ttt0[2]);
arr1[i] = parseFloat(t1);
arr2[i] = parseFloat(t2).toFixed(6);
var ttt1 = t3.split(' ').map(parseFloat);
arr3[i] = new THREE.Vector3(ttt1[0], ttt1[1], ttt1[2]);
// first log
console.log('tt0 '+ttt0[0] + ' ' +ttt0[1] + ' '+ttt0[2] + ' ');
// second log
console.log('arr0'+ arr0[i].x + ' ' +arr0[i].y+ ' ' +arr0[i].z);
}
//third log
show();
});
//....
sceneScreen = new THREE.Scene();
for (var j = 0; j < 2; j++) {
var geometry = new THREE.SphereGeometry(1, 20, 20);
var tmpe = new THREE.Mesh(geometry, new THREE.MeshPhongMaterial({ color: 0xDC143C }));
tmpe.position.set(arr0[j].x, arr0[j].y, arr0[j].z);
tmpe.scale.set(arr2[j], arr2[j], arr2[j]);
// fourth log
console.log('arr0['+j+'] '+arr0[j].x+' ' + arr0[j].y+' ' +arr0[j].z)
sceneScreen.add(tmpe);
}
}
When I call the fourth log function, it shows arr0[0] 0 0 0 and arr0[1] 0 0 0. And I only get a sphere lies in the position (0, 0, 0) of the screen.
It seems that I still cannot pass the txt values to the arrays. How can I solve it? Does I need to load the txt file before my function init()?