Arno van Oordt
Arno van Oordt

Reputation: 3520

Phaser inputEnabled Sprite has mouseover even if not on stage

I found that if I add a Sprite (with inputEnabled = true) to a group and don't add the group to the stage, I can still interact with the Sprite (although it's not shown).

var group = App.phaser.add.group(null, null, false, false);
var bmd = App.phaser.add.bitmapData(100, 100);
bmd.ctx.beginPath();
bmd.ctx.rect(0, 0, 100, 100);
bmd.ctx.fillStyle = '#ff9900';
bmd.ctx.fill();
var sprite = App.phaser.add.sprite(0, 0, bmd);

group.addChild(sprite);
// group.visible = false;

sprite.x = 100;
sprite.y = 100;
sprite.inputEnabled = true;
sprite.input.useHandCursor = true;

The stage is completely blank (which is good). But when I move my mouse over the top left corner I see a mousecursor (and any added event handlers would also respond). Only way to prevent this form happening is to set the group's visibility to false, but this is clearly not the best solution. I'm doing somthing wrong or this is a bug in Phaser?

Upvotes: 0

Views: 687

Answers (1)

PhotonStorm
PhotonStorm

Reputation: 3385

Sprites don't have to be on the display list in order to be interactive, they just have to have 'inputEnabled' set on them. This is commonly used to allow you to create 'invisible' hit areas.

If you want the sprite to be ignored for input you can call sprite.input.stop() and toggle it back on as needed with start.

Also please use Group.add otherwise the sprite doesn't get assigned a z value, throwing it out of sequence in the Group.

Upvotes: 1

Related Questions