Jump to content

How to add delays in Phaser?


toto88x
 Share

Recommended Posts

Hi,

 

What is the best way to add some kind of delay in Phaser? Some examples:

- Move a sprite to position x, wait 2 seconds, then move sprite to another position

- Every time the player die: wait 1 second, then make it reappear at the beginning of the level

- Set a variable to true, then 3 secondes later set it to false

 

Thanks!

Link to comment
Share on other sites

Just last night i was looking for this and found it on the forums already, here is the thread : http://www.html5gamedevs.com/topic/1870-in-game-timer/

 

or to paste the relevant post here, here is the code for an example:

 

 

 

 This is a basic timer that displays the time since the game started.  You could modify it to suit your needs.  

var game = new Phaser.Game(800, 600, Phaser.AUTO, { preload: preload, create: create, update: update });function preload() {game.load.bitmapFont('desyrel', '/assets/fonts/desyrel.png', '/assets/fonts/desyrel.xml');}var textStyle = { font: '64px Desyrel', align: 'center'};var timer;var milliseconds = 0;var seconds = 0;var minutes = 0;function create() {timer = game.add.bitmapText(250, 250, '00:00:00', textStyle);}function update() {//Calling a different function to update the timer just cleans up the update loop if you have other code.updateTimer();}function updateTimer() {minutes = Math.floor(game.time.time / 60000) % 60;seconds = Math.floor(game.time.time / 1000) % 60;milliseconds = Math.floor(game.time.time) % 100;//If any of the digits becomes a single digit number, pad it with a zeroif (milliseconds < 10)milliseconds = '0' + milliseconds;if (seconds < 10)seconds = '0' + seconds;if (minutes < 10)minutes = '0' + minutes;timer.setText(minutes + ':'+ seconds + ':' + milliseconds);}

 

Hope this is enough to give you what you needed.

Link to comment
Share on other sites

Basically, you have to record the time via game.time.now at the event that you want to initiate a delay. Do this by storing it in a variable:

var timeCheck;function myFunction() {//do somethingtimeCheck = game.time.now;}

Then in the update() loop, test for the elapsed time you want to wait against the difference between game.time.now and timeCheck:

function update() {// test for 3 second delayif (game.time.now - timeCheck > 3000){//3 seconds have elapsed, so safe to do somethingmyNextFunction();}else{//still waiting}

There are other ways to accomplish this that are specific to what you are doing (Phaser tweens, setTimeout, setting game.paused)

Link to comment
Share on other sites

  • 4 months later...

Hello Zoombox, Thanks for your replay but the question is not "how to add timer". The question is how to add delay in object / sprite. I had already tried setTimeout function of javascript and its not working. Jquery Delay is also not working. so that's why I was asking..  

I am creating a game in which different nodes are showing and train is going on each node. Now I need to stop the train for a particular time being. so this is my problem. 

By the way thanks for your answer and support.. if you have any suggestion please let me know.

 

Thanks

Link to comment
Share on other sites

That's still right above your post. Pato-reilly answered it.

 

I was writing an answer but I noticed that it is excatly what pato_reilly wrote.

 

Edit: And, of course, I have already done something like this and it works perfectly.

Link to comment
Share on other sites

  • 3 years later...
  • 3 months later...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...