Search the Community

Showing results for tags 'timer'.

More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


  • HTML5 Game Coding
    • News
    • Game Showcase
    • Facebook Instant Games
    • Web Gaming Platform
    • Coding and Game Design
  • Frameworks
    • Phaser 3
    • Phaser 2
    • Pixi.js
    • Babylon.js
    • Panda 2
    • melonJS
  • General
    • General Talk
  • Business
    • Collaborations (un-paid)
    • Jobs (Hiring and Freelance)
    • Services Offered

Find results in...

Find results that contain...

Date Created

  • Start


Last Updated

  • Start


Filter by number of...


  • Start



Website URL





Found 68 results

  1. I am trying to implement some random events happening in my game. For example I want to drop an item on every 5 to 10 seconds. In phaser there is an easy way to create loop events, but they all loop at a certain time. What I am doing so far to achieve this is to add a delay on the event in the timer callback. Is there a smart way to create create events looping in some time range? // Initialize drop timervar cooldown =, this.coolMax);this.dropTimer =;this.dropEvent = this.dropTimer.loop(Phaser.Timer.SECOND * cooldown, this.dropItem, this);this.dropTimer.start();// dropItem function Grabbable.prototype.dropItem= function() { // Do something // Reset timer var cooldown =, this.coolMax); this.dropEvent.delay = Phaser.Timer.SECOND * cooldown;};
  2. I have a side scrolling game I am working on where vehicles coming from the right and are destroyed once off screen on the left. To do this I created a timed event for how often they should come in and add a random vehicle. I create a sprite every time the event is triggered. I was wondering if there's a better way of doing this? (Eg by reusing sprites but also making sure the vehicles don't overlap each other at anytime) It runs fine on my browser and mobile but I just want the best performance. Are there also drawbacks of using timed events to add sprites? Thanks in advance. This is an example of how the vehicles are added below addVehicle:function(){ //console.log('add');; this.vehicles.push(new Vehicles(, this.player, this.posArray, this.prevW)); this.prevW = Number(this.vehicles[this.vehicles.length - 1].width)*2; this.vehEvent = * game.rnd.realInRange(1,1.2), this.addVehicle, this); }
  3. AI enemies do things from time to time, etc ... It is time in my game, I have to create enemies. I do not know how to deal with the issue, I would like you to tell me some options there. How to make the enemy walk to the left and to the right, jump from time to time, etc .... that sort of thing. Thanks!!
  4. I have a timer that creates stars every 800 ms. timer =, spawn_stars, true);It works great, but when the window loses focus the timers keep going. When focus is restored, all the stars that were spawned while the window was unfocused all show up at the same time (so there could be 100+ stars showing up at once, which isn't ideal for this game). This happens whether game.stage.disableVisibilityChangeis set to true or false. The timers stop properly when I use the game.paused function. Is this a bug, or is there something I'm not doing correctly?
  5. fubeca6

    Blinking Tile

    Good Evening, I'm trying to create a blinking tile. Here's what I have so far: function create() { this.timer = game.time.create(false); this.timer.loop(1000, updateCounter, this); this.timer.start();}function updateCounter() { if (this.burn) { this.burn.kill(); } else { createBurn(); }}function createBurn() { this.burn = game.add.sprite(224, 352, 'burn'); game.physics.enable(this.burn, Phaser.Physics.ARCADE); this.burn.body.immovable = true;}However, when I run this, it simply creates the tile over and over without ever killing it -____- I've toyed around a lot with the Phaser Timer examples, etc. but haven't been able to get it quite right. Any help would be greatly appreciated! Thanks
  6. Hi, Sorry if this has been answered before but I couldn't seem to find an answer. I'm using a timer to give my player a set amount of time to complete some levels. Depending on certain actions I want to be able to give the player more or less time as the clock counts down but can't seem to find out how. I'm using this code to set up my timer: this.timer =;this.timer.loop(timerSeconds * 1000, this.timeUp, this);this.timer.start();Is it possible to update this with an extra 5 seconds or 5 seconds less for example while it's counting down? Thanks.
  7. Hello all, I used the timer successfully in my last game using version 1.16. Decided to try 2.0 on my next game, and it seems something has changed. The timer doesn't seem to be firing every 2 seconds, as specified in the create function. Instead, it's calling createSoldier function every frame. Appreciate any help, thanks. create: function () { // Start arcade physics engine this.physics.startSystem(Phaser.Physics.ARCADE); this.gameBg = this.add.sprite(0, 0, 'gameBg'); this.enemySoldiers =; // Enemy create timers this.timer1 =; this.timer1.loop(2000, this.createSoldier, this); this.timer1.start(); }, update: function () { }, createSoldier: function () { // Create soldier inside soldier group this.enemySoldier = this.enemySoldiers.create(Math.random() * 768, 0, 'enemySoldier'); this.physics.enable(this.enemySoldier, Phaser.Physics.ARCADE); // Speed at which mannas drop this.enemySoldier.body.velocity.y = 20; }
  8. Howdy, Please excuse any ignorance of this, I checked the Tanks example but I think I am after something a bit different, yet simpler: I have a platform and I want to have 'enemies' moving randomly left and right. With my current code, I just have 1 enemy (the plan was to just get one working and then look at groups/objects later), it creates and starts moving using a timer but once the timer hits its first iteration the game stops and I get the following console error: Uncaught TypeError: Cannot read property 'apply' of undefined phaser.js:40106 This is the code I have in the create function for the droid // Create Droiddroidsprite = this.add.sprite(, 450, 'droid');droidsprite.animations.add('walk');this.physics.arcade.enable(droidsprite);droidsprite.enableBody = true;droidsprite.body.bounce.y = 0.2;droidsprite.body.gravity.y = 500;droidsprite.body.collideWorldBounds = true;// Droid Movement Timer * 3, 10, moveDroid(), game);Then I have a separate function to handle the movement: function moveDroid() { //randomise the movement droidmover = game.rnd.integerInRange(1, 3); //simple if statement to choose if and which way the droid moves if (droidmover == 1) { droidsprite.body.velocity.x = 100;'walk', 20, true); } else if(droidmover == 2) { droidsprite.body.velocity.x = -100;'walk', 20, true); } else { droidsprite.body.velocity.x = 0; droidsprite.animations.stop('walk', 20, true); } }Is it the fact I am using a Sprite or maybe I should have something in the update function? I am banging my head here as I thought the timer would just simply keep going? Thanks in advance.
  9. Quick question about using the timer and loop in Phaser. I'm trying to get a program that runs a timer that checks how long the user has been playing the game, with the time displayed and it needs to be able to pause when a menu is opened. This is what I have currently: function create() { timer = game.add.text(250, 150, '0'); this.currentTimer = new Phaser.Timer(game, false); this.currentTimer.loop(Phaser.Timer.SECOND, this.updateTimer, this); this.currentTimer.start();}function update() { if(pauseButton.isDown){ timer.setText('Pause'); this.currentTimer.pause(); } if(resumeButton.isDown){ this.currentTimer.resume(); timer.setText(counter); test++; }}function updateTimer() { counter++; timer.setText(counter);}Using the console log I can tell that the timer is starting correctly, and can be paused and then resumed. However, the text does not update except to Pause when I press pause. Anyone know a solution to this?
  10. Basically I'm making a basic racing game where I have the timer start at the beginning of the game, but I can't get it to stop when the racecar collides with the finish line. Any help would be appreciated.
  11. I have problem with timer - I want a repeated execution of a function, which works if the function is lambda, but not if the function has a name. For illustration, this code writes the console every second: game.module('game.main') .body(function() { SceneGame = game.Scene.extend({ init: function() { this.addTimer(1000, function() { console.log('second'); }, true); } }); game.start(); });This code writes the console exactly once, after a second: game.module('game.main') .body(function() { SceneGame = game.Scene.extend({ init: function() { this.addTimer(1000, this.my_timer(), true); }, my_timer: function() { console.log('second'); } }); game.start(); });In my understanding they should be equal, but I have subpar knowledge of JS, so maybe it's some scoping thing?
  12. I have a group of enemies that I loop through and perform various actions with, in my update loop. Basically it checks if each enemy in the group is either at a wall and should turn back, or at a ledge of a platform, and should either 1) turn back, 2) jump, 3) fall off the ledge. The problem I'm having is that nothing is working properly as the update of course happens 60 times a second, and it rapidly checks the conditions and makes decisions. My attempted solution is to attach a timer to each of the enemies in the group, and check if the timer is expired before attempting these actions. Then in the actions function I start the timer, which should run for a couple of seconds. The theory being that after taking an action, it should delay attempting again (per enemy) and by that time the enemy has moved on and the condition (at the ledge, etc.) has passed. I can't figure out a) if this possible and how to do it in Phaser. Since the timer object seems to require a callback, I thought I would make a callback function set/change a flag on the enemy that tracked if it could perform the action or not. I have code like this: Enemy creation: spawnSlime: function() { // create a new slime var yellowSlime = this.slimes.create(760 + Math.random() * 100, 70 + Math.random() * 60, 'medSlime'); yellowSlime.anchor.setTo(0.5, 0.5); yellowSlime.animations.add('slime-walk', [0, 1, 2, 4, 3, 2, 1, 0], 6, true); this.physics.arcade.enable(yellowSlime); this.physics.arcade.collide(yellowSlime, this.layer_world); yellowSlime.body.gravity.y = this.GRAVITY; yellowSlime.body.collideWorldBounds = true;'slime-walk'); yellowSlime.speed = -(Math.random() * 40); // create a timer for each slime yellowSlime.actionTimer =; yellowSlime.actionTimer.loop(2000, function(){yellowSlime.actionReady=true;}, this); // ready for actions, which the timer will be used to provide a pause between actions yellowSlime.actionReady = true; yellowSlime.body.velocity.x = yellowSlime.speed; // console.log(yellowSlime.body.velocity.x); yellowSlime.slimeID = this.slimeCounter += 1; console.log('new slime ID: '+yellowSlime.slimeID) },As you can see I attempt to attach a timer to the slime there, and a callback anonymous function which changes the actionReady state. Here is the code for the slime actions: // slime actions this.slimes.forEach(function(thisSlime){ // collide all slimes with world this.physics.arcade.collide(thisSlime, this.layer_world); this.physics.arcade.overlap(thisSlime, this.lavas, this.lavaDamage, null, this); // Slime meets obstacle or ledge.. either turn back, fall off ledge, or jump console.log('actionReady: '+thisSlime.actionReady); if (thisSlime.actionReady) { if (thisSlime.body.blocked.down) { // slime is on the ground // don't check this slime again for 2 seconds thisSlime.actionReady = false; console.log('about to start timer'); thisSlime.actionTimer.start(); if (thisSlime.body.blocked.left) { // go right thisSlime.body.velocity.x = thisSlime.speed; } else if (thisSlime.body.blocked.right) { // go left thisSlime.body.velocity.x = -thisSlime.speed; } else if (this.tileGet(thisSlime, this.layer_world, 'right') == 0) { // slime on ledge, drops off to the right if ((Math.random() * 3) > 2) { // 1 in 3 chance to jump, otherwise fall off ledge // make slime jump thisSlime.body.velocity.y += 60; console.log('slime'+thisSlime.slimeID+' is jumping'); } else { thisSlime.body.velocity.x = -thisSlime.speed; console.log('slime'+thisSlime.slimeID+' is going back left'); } } else if (this.tileGet(thisSlime, this.layer_world, 'left') == 0) { // slime on ledge, drops off to the left if ((Math.random() * 3) > 2) { // 1 in 3 chance to jump, otherwise fall off ledge // make slime jump thisSlime.body.velocity.y += 60; console.log('slime'+thisSlime.slimeID+' is jumping'); } else { thisSlime.body.velocity.x = thisSlime.speed; console.log('slime'+thisSlime.slimeID+' is going back right'); } } else { // no conditions met, keep actionReady as true thisSlime.actionReady = true; } } }So this function checks the actionReady boolean, and then attempts the actions by first checking the conditions (at a wall or ledge). I have a tileGet function that checks what tile is beyond the ledge, which works. If at a wall it just turns around. However if at a ledge, it makes a random choice for turn back | jump | continue and fall off ledge. As mentioned the timer starting is supposed to change the condition for a couple of seconds so that the slime is not checking/making decision rapidly, canceling them out. I'm not getting an error here, but the slimes are just always falling off the ledge, and the condition seems to be rapidly checked, as if the timer isnt working, as evidenced by my console output: When I tried a function instead of an anonymous function, I would get an error. The call: yellowSlime.actionTimer.loop(2000, this.readyForAction(yellowSlime), this);and the function: readyForAction: function(entity) { entity.actionReady = true; },Any help would be appreciated. Keep in mind that I'm not an expert in javascript, I'm still learning, so there may well be js problems - and I'm not quite sure if/how the Phaser timer function can be used for this. thanks Chris
  13. Long story short: new to Phaser, making a roguelike. Turn-based movement on a tilemap grid, etc. Obviously this ground has been covered before, but it seems that every example of a roguelike or RPG that I've found has basically just set the x/y of the sprite every time a movement occurs, and I'd like to be animating it. I want to wait for user input, then shut off listening for input, and wait for the chosen action/move (if valid) to finish before the game continues processing the turn. I've got it tweening well, but I'm not sure, conceptually, what the best practice is for "waiting." A few games I've seen that use turn-based structure set a variable and then check time in update() to see if sufficient time has passed. I tried using a timer at first, but ran into unexpected behavior (I'm sure I could fix it, but I'm more interested in knowing what I ought to do)... What I have now (experimenting with the former), but not overly enamored with it: function update() { if (acceptInput) { if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) { acceptInput = false; moveTimer = game.time.time; game.add.tween(player.body).to({ x: '-' + TILEWIDTH }, MOVEDURATION, Phaser.Easing.Linear.None, true); } else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) { acceptInput = false; moveTimer = game.time.time; game.add.tween(player.body).to({ x: '+' + TILEWIDTH }, MOVEDURATION, Phaser.Easing.Linear.None, true); } else if (game.input.keyboard.isDown(Phaser.Keyboard.UP)) { acceptInput = false; moveTimer = game.time.time; game.add.tween(player.body).to({ y: '-' + TILEHEIGHT }, MOVEDURATION, Phaser.Easing.Linear.None, true); } else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN)) { moveTimer = game.time.time; acceptInput = false; game.add.tween(player.body).to({ y: '+' + TILEHEIGHT }, MOVEDURATION, Phaser.Easing.Linear.None, true); } } else { if (game.time.time > moveTimer + MOVEDURATION) { acceptInput = true; } }}Thoughts?
  14. when i create a timer: this.gameTimer =;this.gameTimer.start();i can display seconds like this: this.gameTimer.seconds.toFixed(0)but if i pause my timer and resume it, it will not take care of the pause because .seconds is the duration since when the timer start. How can I display the time elapsed since the beginning of the game, taking into account pauses ? also this work perfectly: * 20, 2, removeStar, this);but this not: this.gameTimer =;this.gameTimer.repeat(Phaser.Timer.SECOND * 20, 2, removeStar, this);this.gameTimer.start();events are fired instantly.
  15. Hi guys, I bet this is a really dumb question, but I can't seem to get timers to work. It's in a state based game, and other calls to the game (this) are working perfectly fine. This is the exact error thrown by FireFox. TypeError: this.time.create is not a functionChromium throws: Uncaught TypeError: Object #<Object> has no method 'create' (index):56States.Game.create (index):56d.StateManager.loadComplete phaser.min.js:4d.Game.loadComplete phaser.min.js:5d.SignalBinding.execute phaser.min.js:4d.Signal.dispatch phaser.min.js:4dispatch phaser.min.js:4d.Loader.nextFile phaser.min.js:10d.Loader.fileComplete is my code (if you need to see more let me know) : States.Game = function (game) { ... var timer; };States.Game.prototype = { ... create: function () { ... timer = this.time.create(false); timer.add(500, spawnEnemy, this.context); timer.start(); }, ...}I'm fairly new to JS (I have the most experience with Java, but am not an expert by any means) so there might be some structure issues with the code. Any help would be appreciated.
  16. Gamma

    Timer Issues

    I implemented a repeated event in which calls a function to drop items on a player. Psuedo code is down below: Create a variable that stores a timed repeated event Create a variable that increases a counter for the next timer that drops another set of items onComplete-incremement the timer so that a trigger notifies another timer to make another repeated event My problem is the 'onComplete' method. I've been getting a load of errors for a couple hours of trial and error with no avail. Can anyone explain how this works? I want to know the correct syntax for the onComplete method of a timed event. My trial ended up like this: lvl1 = * 4, 10, this.objectDroppingFunction, this);//Level one drops items on the player lvl1.onComplete(this.incrementCounter, this);//onComplete I call a function that increments the counter//I get the error: Uncaught TypeError: Object [object Object] has no method 'onComplete' Thank you all in advance. I may have also read the documentation wrong. Does the onComplete check to see if a specific timer has finished or ALL timers? If all timers, than how do I "check" individual timers when they stop individually?
  17. EDIT: For those still interested in this issue I have revived it with updated code and such here. Heya, this is my first post so I apologize if this has already been covered in another relevant topic. Essentially I am setting up a HUD prototype. I pass the game object to the HUD initially then prototype all of the relevant functions afterword. I am trying to get a timer setup to essentially display a 1 minute countdown. Each time I try to run the game though I get the error... Uncaught TypeError: Cannot call method 'loop' of undefined Here is the code I am using... ~~~ HUD = function(game) { = game;this.score = 0;this.text = null;//this.timer = null;}; var countDown = 60; HUD.prototype = {create: function() {this.text =, 16, 'Score: 0\nTimer: 01:00', { fontSize: '32px', fill: '#000' });, updateText,;}, update: function() { }}; function updateText() {countDown--; if (countDown < 10) {this.text.content = 'Score: 0\nTimer: 00:0' + countDown;} else {this.text.content = 'Score: 0\nTimer: 00:' + countDown;} if (countDown == 0) {endGame();}} function endGame() {game.state.start('GameOver');}~~~Any thoughts?
  18. Hello, I would like to know how hard is it to make an accurate timer count down. For example, count down for 1 minute. I would also need to tell player the time left every second. I have heard that setTimeout may not be accurate. What is the best way to do this?