toto88x Posted January 6, 2014 Share Posted January 6, 2014 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! parwaniprakash 1 Link to comment Share on other sites More sharing options...
Shadow20 Posted January 6, 2014 Share Posted January 6, 2014 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 More sharing options...
Hsaka Posted January 6, 2014 Share Posted January 6, 2014 Hi, it looks like you can also do this using the Timer class in the dev version of Phaser: https://github.com/photonstorm/phaser/commit/aa3a86df6ef135bcccda0cf390ba7f01e86d0f05timer = game.time.create(1000, false);timer.add(3000);timer.onEvent.add(doSomething, this);timer.start(); Link to comment Share on other sites More sharing options...
pato_reilly Posted January 6, 2014 Share Posted January 6, 2014 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) illscode, Luis Felipe and WombatTurkey 3 Link to comment Share on other sites More sharing options...
parwaniprakash Posted June 6, 2014 Share Posted June 6, 2014 Hello toto88x, I have the same query as you asked in the post. Have you got your answer? if yes please let me know. Thanks for your help in advance. Link to comment Share on other sites More sharing options...
ZoomBox Posted June 6, 2014 Share Posted June 6, 2014 The answer is just above your post... Link to comment Share on other sites More sharing options...
parwaniprakash Posted June 6, 2014 Share Posted June 6, 2014 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 More sharing options...
ZoomBox Posted June 6, 2014 Share Posted June 6, 2014 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 More sharing options...
parwaniprakash Posted June 7, 2014 Share Posted June 7, 2014 Yes its working fine.. thanks for your great support.. Link to comment Share on other sites More sharing options...
samme Posted September 16, 2017 Share Posted September 16, 2017 You don't need to add a new timer, just add a new timer event on the built-in game timer: game.time.events.add(DELAY, function () {/* … */}); Yehuda Katz 1 Link to comment Share on other sites More sharing options...
n7pankake Posted January 15, 2018 Share Posted January 15, 2018 can you add a delay WITHOUT Needing a damn function/event? Link to comment Share on other sites More sharing options...
Recommended Posts