Warning: The magic method EDD_Blocks::__wakeup() must have public visibility in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php on line 101

Warning: The magic method EDD_Blocks::__wakeup() must have public visibility in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks/edd-blocks.php on line 101

Warning: The magic method GAINWP_Manager::__wakeup() must have public visibility in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/gainwp.php on line 78

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 555

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 585

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 617

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 651

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 686

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 745

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 785

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831
{"id":549,"date":"2017-06-28T14:28:07","date_gmt":"2017-06-28T14:28:07","guid":{"rendered":"https:\/\/phasergames.com\/?p=549"},"modified":"2017-06-30T12:18:51","modified_gmt":"2017-06-30T12:18:51","slug":"phaser-timer-basics-tutorial","status":"publish","type":"post","link":"https:\/\/phasergames.com\/phaser-timer-basics-tutorial\/","title":{"rendered":"Phaser Timer Basics Tutorial"},"content":{"rendered":"

How to use Phaser Timers<\/h3>\n

Timers can be a big part of game development. They can set up how often you want to release monsters or delay the next part of the game from appearing until your player reads a message. Timers can also be used to build clocks that can count up or down and Phaser has what you need to put together several different options for timers. Phaser Timer Snippets can be found under miscellaneous\u00a0snippets.<\/a><\/p>\n

One-Shot Timers<\/h3>\n

A one-shot timer is a timer that will fire an event once after a specified number of seconds. You can also think of it as a delay timer. This example below will call a function called delayOver after 5 seconds.<\/p>\n

game.time.events.add(Phaser.Timer.SECOND*5, this.delayOver, this);<\/pre>\n

Looping Timer<\/h3>\n

A looping timer works in much the same way as the one-shot but it will keep firing the function until stopped. This code will call a function called addMonster every second<\/p>\n

game.time.events.loop(Phaser.Timer.SECOND, this.addMonster, this);<\/pre>\n

Stopping a Timer<\/h3>\n

To stop a timer you’ll need to add a reference to it first by assigning it to a variable.<\/p>\n

this.monsterTimer=game.time.events.loop(Phaser.Timer.SECOND, this.addMonster, this);<\/pre>\n

Then when you want to stop the timer you can turn it off like this:<\/p>\n

game.time.events.remove(this.monsterTimer);<\/pre>\n

Adjusting Time<\/h3>\n

Phaser timers measure in milliseconds. 1000 milliseconds is equal to 1 second. Using the Phaser.Timer.SECOND is the same as just placing 1000 as the first parameter but using the constant makes it easier to read. By multiplying or dividing this constant we can adjust the frequency the function is called.<\/p>\n

    \n
  • Phaser.Timer.SECOND =1 second<\/li>\n
  • Phaser.Timer.SECOND*5 =5 seconds<\/li>\n
  • Phaser.Timer.SECOND\/2= half a second or call the function twice a second<\/li>\n
  • Phaser.Timer.SECOND\/10 =one tenth a second<\/li>\n<\/ul>\n

    Building a Countdown Timer<\/h3>\n

    Say that you want to give the user a certain number of minutes or seconds to complete a level or a game. You can achieve this by setting a one shot timer to call the game over. If you want to display the number of seconds left, however, you’ll need to set up a countdown clock. We can do this by setting up a variable to hold the seconds and then subtracting one per second on a loop timer. We can also convert the number of seconds to minutes. Here is some sample code to do that:<\/p>\n

    var StateMain = {\r\n    preload: function() {},\r\n    create: function() {\r\n        \/\/total time until trigger\r\n        this.timeInSeconds = 120;\r\n        \/\/make a text field\r\n        this.timeText = game.add.text(game.world.centerX, game.world.centerY, \"0:00\");\r\n        \/\/turn the text white\r\n        this.timeText.fill = \"#ffffff\";\r\n        \/\/center the text\r\n        this.timeText.anchor.set(0.5, 0.5);\r\n        \/\/set up a loop timer\r\n        this.timer = game.time.events.loop(Phaser.Timer.SECOND, this.tick, this);\r\n    },\r\n    tick: function() {\r\n        \/\/subtract a second\r\n        this.timeInSeconds--;\r\n        \/\/find how many complete minutes are left\r\n        var minutes = Math.floor(this.timeInSeconds \/ 60);\r\n        \/\/find the number of seconds left\r\n        \/\/not counting the minutes\r\n        var seconds = this.timeInSeconds - (minutes * 60);\r\n        \/\/make a string showing the time\r\n        var timeString = this.addZeros(minutes) + \":\" + this.addZeros(seconds);\r\n        \/\/display the string in the text field\r\n        this.timeText.text = timeString;\r\n        \/\/check if the time is up\r\n        if (this.timeInSeconds == 0) {\r\n            \/\/remove the timer from the game\r\n            game.time.events.remove(this.timer);\r\n            \/\/call your game over or other code here!\r\n            this.timeText.text=\"Game Over\";\r\n        }\r\n    },\r\n    \/**\r\n     * add leading zeros to any number less than 10\r\n     * for example turn 1 to 01\r\n     *\/\r\n    addZeros: function(num) {\r\n        if (num < 10) {\r\n            num = \"0\" + num;\r\n        }\r\n        return num;\r\n    },\r\n    update: function() {}\r\n}<\/pre>\n

    Countdown Clock Example<\/h3>\n\n\n