Hello, another newbie question here, hope somebody with experience can answer this!
I have a sprite with bitmap attached to it, there are several buildings that needs to be / appear “interactive”. I need to detect 3 mouse events : rollover, rollout and click.
So in my mind I could think of these 2 scenarios of how to achieve this:
-
create actual “dummy” rectangle shapes with 0 alpha and overlay these on the top of bitmap.
-
define these hotspots programmatically and in global input handler check if the cursor is within the bounds of these rectangles.
Tried the approach nu 1:
with this code: (I’m using TypeScript)
let settingsGFX = this.add.graphics();
settingsGFX.fillStyle(0x0000ff, .3);
settingsGFX.fillRect(setting_rect.x, setting_rect.y, setting_rect.width, setting_rect.height);
settingsGFX.setInteractive({useHandCursor:true, hitAreaCallback: ()=>this.setActiveSection("settings")});
…
private setActiveSection( section :string):void
{
var pointer : Pointer = this.input.activePointer;
if (pointer.isDown)
{
console.log(section);
console.log(pointer);
}
}
(full code here : https://pastebin.com/EyD98y7Y )
While this works kinda ok with click event, the problem I have is to detect mouse over and out events, as well as I am not sure how to define the hitArea, as by default I would expect the settingsGFX as a whole to be considered as hit area?
So to recap , I guess my too questions are:
-
what’s the best way to achieve multiple hotspots on a single sprite containing and image?
(with mouseover / mousedown / mouseout events) -
if I use the described method… how do I correctly define the hotspot in the setInteractive handler, and get mouse over and out working as well?