Jump to content

How to access text element when it's clicked?


alessa
 Share

Recommended Posts

I have two texts on my interface, one used to start the game and the other one for some calibration.

How can I get access to the element that has been actually clicked? Here's my code for now..

this.startText = this.add.text(400, 300, 'START', {
  fill: 'white',
  fontFamily: CONST.fonts.default,
  fontSize: 48
})

this.otherText = this.add.text(400, 300, 'CALIBRATION', {
  fill: 'white',
  fontFamily: CONST.fonts.default,
  fontSize: 48
})

// make the text interactive
this.startText.setInteractive(new Phaser.Geom.Rectangle(0, 0, this.startText.width, this.startText.height), Phaser.Geom.Rectangle.Contains);

// listen for clicks on it
this.input.on('pointerdown', function (pointer){
  // here I should have something like "if text is START, then do something; if it's CALIBRATION, do something else"	
});

Any tip in merit? Cheers!

Link to comment
Share on other sites

Instead of this.input.on... use:

this.startText.on('pointerdown', function () {
    // this here refers to this.startText, so you can do e.g. this.setText("new text");
})

This works on Images, so I expect it to work also on text and other game objects.

Link to comment
Share on other sites

36 minutes ago, YuPi said:

Instead of this.input.on... use:


this.startText.on('pointerdown', function () {
    // this here refers to this.startText, so you can do e.g. this.setText("new text");
})

This works on Images, so I expect it to work also on text and other game objects.

Indeed it worked, thanks! Is there a place where these kind of documentation is easily accessible for a total beginner? 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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