Jump to content

Two events


Theasker
 Share

Recommended Posts

Hello, I newbie in phaser, I coding a game words. The words are displayed and then illuminated one by one.
Each time a word is turned on, after this word turn off, and the next one turns on.. I try this but don't work_

 

			this.game.time.events.repeat(level.time2, level.highlightNum, function (){
				texts[counter].fill = "black";
				this.game.time.events.repeat(level.time2, 1, function (){
					texts[counter].fill = "orange";
				}, this);
				counter++;
			}, this);

I try too, with javascript only, but it does not work either:

			for(var x = 0;x<level.highlightNum;x++){
				setTimeout(function() {
					texts[counter].fill = "black";
					counter++;
					console.log('counter: ', counter);
				}, 500);
			}

I don't know what to try.

Thanx in advance.

Mauri (sorry for my english)

Link to comment
Share on other sites

a for loop is synchronous, meaning you've stacked up `level.highlightNum` amount of timeouts that will all fire in roughly 500ms, whereas I think you want them to fire after 500, 1000, 1500 etc ms.

var counter = 10
function countdown () {
  console.log('counter:', counter)
  if (--counter) {
    setTimeout(countdown, 500)
  }
}

countdown()

This way has a downside, that filthy dirty variable out there, yuck, we can solve that in a couple of ways:

function countdown (count) {
  console.log('counter:', count)
  if (count) {
    setTimeout(function () {
      countdown(--count)
    }, 500)
  }
}

countdown(10)

Or (with a little bit of es6 sugar)

const countdown = (count, time) => () => {
  console.log(`count: ${count}`)
  if (!count) {
    return
  }
  setTimeout(countdown(--count, time), time)
}

const startCount = countdown(10, 500)
startCount()

 

Link to comment
Share on other sites

  • 4 weeks later...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...