normanku Posted August 15, 2015 Share Posted August 15, 2015 Hi, I'm having problems detroying sprites created a few seconds before. I want to show user a text box showing some information when he press Space, then the textbox dissapear automatically after 3 seconds.if(action.isDown){ diag1 = game.add.sprite(140,160,'diag1'); //wait a few seconds... diag1.destroy();} I've tried so many ways to stop the program, but diag1.destroy(); still executing inmediatly after add.sprite Link to comment Share on other sites More sharing options...
drhayes Posted August 18, 2015 Share Posted August 18, 2015 That's how JS works, it's a run-to-completion language. If you *could* stop the program for three seconds then nothing else would animate and, likely, the browser would kill your script. In Phaser you can schedule events to run in the future like this:game.time.events.add(DELAY_IN_MILLISECONDS, callback, context, arg1, arg2); Tilde 1 Link to comment Share on other sites More sharing options...
CodeToWin Posted August 18, 2015 Share Posted August 18, 2015 another kludge for this problem: when you are dealing with finite groups (of bullets, say) you don't want the player to fire off the entire group of bullets at once, so you can use a timer to space out the firing events. In your case, the timer can be used to delay the destruction of your sprite.function update() { if (timer > game.time.now && diag1Flag) { diag1.destroy(); } if (action.isDown) { diag1 = game.add.sprite(....); diag1Flag = true; timer = game.time.now + 3000; // 3000 ms from now }} Link to comment Share on other sites More sharing options...
Recommended Posts