npilla Posted October 29, 2013 Share Posted October 29, 2013 I want to add an automatic timer that is visible in the game and automatically stops when all sprites(enemies) are killed. Not sure if I'm looking in the wrong spot but couldn't find any examples. Thank you! hollygood 1 Link to comment Share on other sites More sharing options...
rich Posted October 29, 2013 Share Posted October 29, 2013 When you say timer I assume you mean something that counts up in minutes and seconds? Just checking before I make an example. Link to comment Share on other sites More sharing options...
npilla Posted October 30, 2013 Author Share Posted October 30, 2013 Yes, just a visible timer in the upper-left corner that counts up or down while the game is running and then stops when the enemy group has been completely killed. The idea is that whoever kills the alien group the fastest wins (super basic). I'm using the shoot to pointer example to have a ship in the middle spin and kill aliens that are randomly drawn to the screen. Eventually I'd like to have the aliens converge on the center and kill the ship on first collision (work in progress). Thank you for the help!!! Link to comment Share on other sites More sharing options...
Brendan Posted December 4, 2013 Share Posted December 4, 2013 does anyone have an example of how to use Timer? i'm new to phaser and i cannot find an example in the reference. Link to comment Share on other sites More sharing options...
Zeterain Posted December 4, 2013 Share Posted December 4, 2013 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 zero if (milliseconds < 10) milliseconds = '0' + milliseconds; if (seconds < 10) seconds = '0' + seconds; if (minutes < 10) minutes = '0' + minutes; timer.setText(minutes + ':'+ seconds + ':' + milliseconds);} scootersda, peacock, shawnbless and 4 others 7 Link to comment Share on other sites More sharing options...
Zeterain Posted December 4, 2013 Share Posted December 4, 2013 For a timer that stops when all the sprites in a group are killed, try this:function update() { if (yourGroup.countLiving() > 0) updateTimer();}Now when everything in yourGroup is killed, the timer won't be updated any more. Note that the timer's current minute/second/millisecond variables are globally accessible and will contain the time at last update. Does that all make sense? If you have any questions about any of what I've written let me know. shawnbless 1 Link to comment Share on other sites More sharing options...
Brendan Posted December 5, 2013 Share Posted December 5, 2013 Thanks for taking the time to reply zeterian this makes sense and is a great timer, so do we need to write our own timers for phaser? I was hoping to be able to pause the timer and also use it as a countdown, e.g. to call a function when the timer reaches 0 (to end the game if all objects have not been destroyed). Looking at the documentation i can see a class called Timer and a class called Time, i was hoping there were ready made timers we could use. There are no examples for these in use in the documentation (i can see you use game.time.time in your example), i have seen this in one of the phaser examples too but its not quite what i was looking for. I was hoping there would jbe a way to just add a timer from the phaser framework, start it (and pause it when needed e.g. if the player collects an extra time power up) and then call a function when the timer is complete, rather than write my own timer function. Link to comment Share on other sites More sharing options...
rich Posted December 5, 2013 Share Posted December 5, 2013 There is indeed a built-in Timer, but it's not fully fledged yet so for now it might be better to use the above approach. However if you want to test it, this is how you use it:timer = new Phaser.Timer(game);timer.add(delayInSeconds, parameter1, parameter2, ...);// Add as many events as you like here, they are popped off the stack once the time is hittimer.onEvent.add(doSomething, this);timer.start();function doSomething(parameter1, parameter2, ...){ }You can display the time with Timer.seconds It doesn't (yet) respect the game paused state, but soon will do. kass and Arlefreak 2 Link to comment Share on other sites More sharing options...
Brendan Posted December 6, 2013 Share Posted December 6, 2013 Thanks rich Link to comment Share on other sites More sharing options...
mamwalter Posted February 1, 2014 Share Posted February 1, 2014 Hi, I tried to use built-in Timer but maybe i missed something, because event never triggered. this.timer = new Phaser.Timer(this.game); this.timer.add(3); this.timer.onEvent.add(this.nextMove, this); this.timer.start(); nextMove: function() { console.log("event") this.moveToCoords(250, 250) }, Link to comment Share on other sites More sharing options...
XekeDeath Posted February 1, 2014 Share Posted February 1, 2014 Are you using the dev branch? You should check to see if your timer is being updated, I suspect that it is not.I once made a timer in the same way you are, and it never updated because it was not in the games array or timers to update.I ended up creating my timer like this: this.timer = this.game.time.create(this.game); this.timer.add(this.delay, this.readyForAction, this); this.timer.start(); Link to comment Share on other sites More sharing options...
mamwalter Posted February 2, 2014 Share Posted February 2, 2014 i use phaser 1.1.3 (bower version). when i print seconds like this :this.game.debug.renderText('timer:' + this.timer.seconds(), 32, 70, 'rgb(0,0,0)');timer updates.Your solution dont work for me: <Object> has no method 'create'. Link to comment Share on other sites More sharing options...
XekeDeath Posted February 2, 2014 Share Posted February 2, 2014 That is because I am using the dev version of 1.1.4, which has all the new fancy timer code in it. Link to comment Share on other sites More sharing options...
mamwalter Posted February 4, 2014 Share Posted February 4, 2014 should i try the dev version ? Link to comment Share on other sites More sharing options...
XekeDeath Posted February 5, 2014 Share Posted February 5, 2014 It won't be the dev version for much longer, so it would be a good idea to get onto it now.There are many changes from 1.1.3 to 1.1.4, you will need to make sure everything you have doesn't break when you update. Link to comment Share on other sites More sharing options...
scootersda Posted August 30, 2015 Share Posted August 30, 2015 First time on this site, and based on this post, signed up right away. Good stuff...just what I needed as I am leaping from the world of ASP.Net web forms into MVC, HTML, CSS and Javascript. My prohect needed a timer...and this post and related ones saved me a ton of reinventing the wheel (even if I could). Link to comment Share on other sites More sharing options...
Recommended Posts