29 lines
721 B
TypeScript
29 lines
721 B
TypeScript
type HoverCallback = (hovering: boolean) => void;
|
|
export function hoverEffect(
|
|
gameObject: Phaser.GameObjects.GameObject,
|
|
callback: HoverCallback,
|
|
) {
|
|
let isHovering = false;
|
|
const onPointerOver = () => {
|
|
if (isHovering) return;
|
|
isHovering = true;
|
|
callback(true);
|
|
};
|
|
const onPointerOut = () => {
|
|
if (!isHovering) return;
|
|
isHovering = false;
|
|
callback(false);
|
|
};
|
|
|
|
gameObject.on("pointerover", onPointerOver);
|
|
gameObject.on("pointerout", onPointerOut);
|
|
const cleanup = () => {
|
|
gameObject.off("pointerover", onPointerOver);
|
|
gameObject.off("pointerout", onPointerOut);
|
|
gameObject.off("destroy", cleanup);
|
|
};
|
|
|
|
gameObject.once("destroy", cleanup);
|
|
return cleanup;
|
|
}
|