Phaser: Graphic setInteractive does not work correctly

Created on 27 Nov 2018  路  2Comments  路  Source: photonstorm/phaser

Version

  • Phaser Version: 3.15.1
  • Operating system: Not specifically(Mac & Win)
  • Browser: Not specifically(Firefox & Chrome)

Description

graphics setInteractive not work correctly.
suppose: console outputs: 'btn pointerup'
result: nothing happened

Example Test Code

preload() {
    console.log('now loading...');
}

create() {
    var btn = this.add.graphics();
    btn.fillStyle(0xffff00, 1);
    btn.fillRoundedRect(32, 32, 300, 200, 32);

    // btn.setInteractive(new Phaser.Geom.Rectangle(0, 0, 300, 200));

    btn.setInteractive();
    btn.on('pointerup', function() {
        console.log('btn pointerup');
    }, this);
}

Additional Information

If rectangle param provided, will get phaser-3.15.1.min.js:1 Uncaught TypeError: n.hitAreaCallback is not a function error when pointer hover.

Most helpful comment

Graphics object does not have bounds, pass a geom shape

btn.setInteractive(new Phaser.Geom.Rectangle(0, 0, 300, 200), Phaser.Geom.Rectangle.Contains);

to define that bounds.

Or use rectangle shape game object (scene.add.rectangle(x, y, width, height, fillColor).setInteractive())

All 2 comments

Graphics object does not have bounds, pass a geom shape

btn.setInteractive(new Phaser.Geom.Rectangle(0, 0, 300, 200), Phaser.Geom.Rectangle.Contains);

to define that bounds.

Or use rectangle shape game object (scene.add.rectangle(x, y, width, height, fillColor).setInteractive())

@rexrainbow Both suggestions works, thank you.

Was this page helpful?
0 / 5 - 0 ratings