EmptyStone
EmptyStone

Reputation: 315

Phaser Can't get mouse to work right, want text to appear when the cursor hovers over a sprite

I'm trying to have some text appear when the mouse hovers over an item in the invetory.

Here is the code I have for a key appearing in the inventory, disappearing when its used in its quest, and what should make text appear when the player hovers over it:

if(this.pack[0] != null){
        if (this.pack[0] == "key"){
            this.key = this.physics.add.sprite(180, 150, 'key').setInteractive();
            this.key.body.immovable = true;
            this.key.body.allowGravity = false;
            this.key.setScale(4);
            this.key.texture.setFilter(Phaser.Textures.FilterMode.NEAREST);
        }
    }

    this.key.on('pointerdown', function (pointer) {
        this.line1.setText('Peef: Its the key to the bedroom door.');
        this.line2.setText('Bandit hid it under the bad for some reason. Probably just playing.');
    }, this);

    if(tutorial == "complete"){
        this.key.destroy();
    }

Unfortunatly, I can't seem to get the mouse to work at all. I've tried changing some of the words around, from pointerdown to pointerover, or from pointer to gameobject, to moving everything under one if statement or from the update to the create method, but none of them got the result I want. Does anyone know what the correct code for what I want is?

If it helps, I'm using Phaser 3 in VSCode employing arcade physics.

Upvotes: 1

Views: 117

Answers (1)

winner_joiner
winner_joiner

Reputation: 14880

You needd to use an other Event, just use pointerover, that should work.
(link to the documenation)

Like this:
this code should be in the create function, or where the sprite is created. This code should not be called multiple times(like in the update function).

this.key.on('pointerover', function () {
    // ...
});

Alternative:
in the create function

 this.input.on('gameobjectmove', (pointer, gameObject) => {
     if(this.key && this.key == gameObject) {
         // ...
     }
 });

Upvotes: 1

Related Questions