0

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.

winner_joiner
  • 12,173
  • 4
  • 36
  • 61
EmptyStone
  • 235
  • 1
  • 7

1 Answers1

0

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) {
         // ...
     }
 });
winner_joiner
  • 12,173
  • 4
  • 36
  • 61
  • Ok, I changed the code to this.key.on('pointerover', function () { 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); – EmptyStone Jul 06 '23 at 16:15
  • But I still got the same problem. Do I have to move the code into the if statement, or to the create method? – EmptyStone Jul 06 '23 at 16:16
  • the code should be in the `create` function. _(Where all events should be initialized)_ – winner_joiner Jul 06 '23 at 16:19
  • I moved all three code snippets about the key to the create method, but doing so made the key not appear at all. I'm starting to consider ditching the mouse entirely. – EmptyStone Jul 06 '23 at 16:28
  • Well i don't know your code, so I just can say how I would do it. What you can do is put everything back as it was, and put the `this.key.on(...)` into the if, where the sprite is created. if that doesn't work you could use `this.input.on('gameobjectmove', ....)`. I updated my answer – winner_joiner Jul 06 '23 at 16:59
  • Thanks for trying to help, but I'm afraid this has proved too confusing for me. I've decided to ditch using the mouse for this and switch to using the WASD buttons, like the rest of my game. – EmptyStone Jul 06 '23 at 20:55
  • Good Idea, keep thinks simple. Nevertheless the alternative solution should be easy to implements, just 3 Lines of codes. – winner_joiner Jul 07 '23 at 07:10