Hello,
I’m trying to load a collada object and move it forward by changing its position in animate() but I have this error : Cannot read ‘position’ of undefined at animate. I don’t understand, it is the first time I have this error.
That’s what I do :
I declare my var mario outside of all functions :
var mario;
init();
animate();
In init I call my function to load a collada :
CreateObject(mario,"dae/mario.dae",50,50,50,0,-28,0,0);
CreateObject :
function CreateObject(object,src,size1, size2, size3, posX,posY, posZ, rotation,audio){
// loading manager
var loadingManager = new THREE.LoadingManager( function () {
scene.add( object );
});
// collada
var loader = new THREE.ColladaLoader( loadingManager );
loader.load( src, function ( collada ) {
object = collada.scene;
object.scale.set(size1,size2,size3); //Objet permettant de donner une taille à la maison
object.position.x = posX;
object.position.y = posY;
object.position.z = posZ;
object.rotation.z = rotation;
object.traverse(function (child) {//on ajoute les ombres
if (child instanceof THREE.Mesh) {
child.castShadow = true;
child.receiveShadow = true;
}
});
if(audio)
object.add(audio);
});
}
And then in animate I try to move the collada :
function animate() {
requestAnimationFrame( animate );
mario.position.x += 3;
//etc
}
Can someone help me please ?
Thank you.