.setInteractive() hitAreaCallback usage help

Hello!

I noticed that the .setInteractive() method on a few of the GameObjects (e.g. Text) takes an optional argument for a callback function. However, I can’t seem to get it working. I have my Scene defined in an ES6 class, the Text created with the callback function as the second argument, and my callback function defined in the class.
Here’s an example of my usage:

In my create function:

this.example = this.add.text(0,0, "hello").setInteractive({ useHandCursor: true }, this.callback);

Elsewhere in my class:

callback() {
    console.log("woo");
}

Can anyone assist me here? As of now I’m just using “pointerdown” listeners but this method seems more streamlined.

this.add.text(0,0, "hello")
    .setInteractive({ useHandCursor: true })
    .on( 'pointerdown', function(pointer, localX, localY, event){ /* ... */ });

Here is a cheat sheet of touch input.

3 Likes

The callback argument to setInteractive() is a hit area callback, a function that tests whether a location is within the hit area or not (returning true or false). It’s not an input event callback, which you add with on().

Typically the hit area callback is the Contains() function corresponding to the shape argument. See shape hit tests.

1 Like

See text input test.

1 Like

Ah this makes way more sense. Thank you!