Reference Text Object in Scene

Hi,

I am trying to get a text object to blink by using a timer and setting it to not be visible in my scene class. The problem is that I don’t seem to be able to reference the text object.

this.presstart = this.add.text(260, 480, 'Press enter to start', { fontFamily: 'Arial', fontSize: 20, color: '#ffffff' });

this.blinkOff = true;
this.timer = this.time.addEvent({
  delay: 500,
  callback: this.blinkText,
  loop: true
});
console.log(this);

The above is in my create function and then in my update function I have the following:

blinkText: function() {
  console.log(this.presstart);
  if(this.blinkOff) {
    this.presstart.visible = false;
    this.blinkOff = false;
  } else {
    this.presstart.visible = true;
    this.blinkOff = true;
  }
}

In the console, I just get a message, "cannot set visible of undefined. I am having difficulty understanding how the scoping works, but I understand that a lot of this is due to my inexperience with Javascript. Other variables I have defined this way have worked when referenced in other methods. Can anyone help?

addEvent() has a callbackScope config property. You can pass the current context through that.

https://github.com/samme/phaser3-faq/wiki#how-do-i-use-this-in-a-scene

I am also new to Phaser, so I definitely could be wrong, but I’m wondering if the problem could be that the callback calls this.blinkText which is a function registered to this (i.e. the scene), but the blinkText function defined below is not registered to the scene?

Correct me if I’m wrong though, but that’s just my two cents.

Thanks, I added the line “callbackScope: this,” and it is now working.