lundi 16 février 2015

SetTimeOut() in THREE JS


I realize a project for the master degree. I must create a three js space invaders game.


The project is well underway but i have a problem. My aliens ( THREE.Mesh object ) must be able to fire randomly. To carry out that, i've created a function which should draw a random number. This function works. The problem comes the animate() function. In fact i can't put a SetTimeOut() the animate() function.


The SetTimeOut() works the first time animate() is called but after there is no timer. The code executing continually without waiting the timer.


Maybe the problem coming because animate is continually called by requestAnimationFrame();


My code :


Index.html =>



if (!init())animate();

function animate(){
requestAnimationFrame( animate );
level1.animate();

render();
}


Level.js =>



Level.prototype.animate = function()
{

//Timer doesn't work
var that = this;

//Just a test with a simple console.log test
setTimeout(function() { console.log("test"); },10000);*/

this.sky.rotation.x -=0.005;

this.spaceship.fire();
for (var i=0; i<this.ducks.length;i++)
{
this.ducks[i].move();
if (this.ducks[i].is_ready_to_fire())
this.ducks[i].fire_if_ready();
}


};


With this example the program will wait 10 seconds the first time before print "test" and after the first call, print "test" without waiting.


Have you any ideas ?


Thank you very much.


Sorry for my poor english.





Aucun commentaire:

Enregistrer un commentaire