pinkman Posted April 6, 2016 Share Posted April 6, 2016 Hello, multiple timers, it is impossible to use in one gamelevel, at the same time?Simple example: I need a timer for the duration of the game (90 seconds).I need a timer for enemies (short intervals / random).I need a timer for rare, larger enemies (long intervals / random).Do not go there with?var mytime1 = ....var mytime2 = ....var mytime3 = ....I need all 3 timer to the same time. I am so far failed. The Phaser example "Multiple Timers" does not help me further (because there are not for me "Multiple Timers" because run consecutively and is just a trick. Or I do not understand the message).Thanks for hints Link to comment Share on other sites More sharing options...
megmut Posted April 6, 2016 Share Posted April 6, 2016 Hey Pinkman, there are probably several ways you could tackle this. One of them being: init() { this.gameTime = 90; } create() { this.spawnAllowed = true; this.enemyGroup = this.game.add.group(); // create group this.game.time.events.add(1000, timerTick); // timer for game running } timerTick() { this.gameTime--; // decrease game timer by 1 console.log(this.gameTime; if(this.gameTime === 0) { // check if game has finished console.log('game over'); this.game.state.start('gameOver, true, false') // send to game over state } } createNewEnemy() { if (this.spawnAllowed) { // check if spwawning is allowed this.enemyGroup.create(x, y, cacheKey, frame); // add sprite to group queueEnemy(this.game.rnd.integerInRange(2500, 5000); // call enemy queue for random between 2.5 and 5 seconds } } creatNewBoss() { if (this.spawnAllowed) { // check if spwawning is allowed this.enemyGroup.create(x, y, cacheKey, frame); // add sprite to group queueEnemy(this.game.rnd.integerInRange(30000, 45000); // call boss queue for random between 30 and 45 seconds } } queueEnemy(time) { this.game.time.events.addOnce(time, createNewEnemy); // add a timer that gets called once, then auto disposes to create a new enemy after the time given } queuNewBoss(time) { this.game.time.events.addOnce(time, createNewEnemy); // add a timer that gets called once, then auto disposes to create a new enemy after the time given } extraTimeCollected(val) { this.gameTime += val; // if a powerup is collected, give the player more time } stopSpawning(time) { this.spawnAllowed = false; // stop any enemies from being created will work for queued enemies also;=. } pinkman 1 Link to comment Share on other sites More sharing options...
pinkman Posted April 6, 2016 Author Share Posted April 6, 2016 Hello megmut I did not expect that someone can help a with such a great example to me. Even with Enemies. Wow. So I can do new studies. Originally, it was all about the timer. But now I see other errors in the Enemies that would only later noticed. Therefore, I thank you very much. Certainly no intention you have intervened at the right time. Thx! Pinkman Link to comment Share on other sites More sharing options...
Recommended Posts