Jump to content

Disable mouse inputs


Pietari
 Share

Recommended Posts

I have Sprite and Sprite2 and i want to control only one at a time. Right now if i press somewhere on the screen, they both go in the direction of where the mouse was. What i would like to do is have only Sprite move there and if i press on Sprite2 and click somewhere on the screen again, only Sprite2 moves there and Sprite keeps on doing what it was doing before and not care about the mouse click. Is this possible?

Link to comment
Share on other sites

32 minutes ago, fillmoreb said:

It's possible.  Post code demonstrating how you made the sprites go towards the pointer on click, and it'll be easier to give you advice on how.

if (game.input.mousePointer.isDown)
    {
 
        game.physics.arcade.moveToPointer(sprite, 20);


        if (Phaser.Rectangle.contains(sprite.body, game.input.x, game.input.y))
        {
            sprite.body.velocity.setTo(0, 0);
        }
            
    }

Link to comment
Share on other sites

27 minutes ago, fillmoreb said:

Hmmm...  the code you're posting should only move a single sprite. I'm not sure why both of yours are moving.  Can you post the rest of your code?

if (game.input.mousePointer.isDown)
    {
 
        game.physics.arcade.moveToPointer(sprite, 20);


        if (Phaser.Rectangle.contains(sprite.body, game.input.x, game.input.y))
        {
            sprite.body.velocity.setTo(0, 0);
        }
            
    }

if (game.input.mousePointer.isDown)
    {
 
        game.physics.arcade.moveToPointer(sprite2, 20);


        if (Phaser.Rectangle.contains(sprite2.body, game.input.x, game.input.y))
        {
            sprite2.body.velocity.setTo(0, 0);
        }
            
    }

Link to comment
Share on other sites

You will need to keep track of which sprite is currently 'selected', and only do moveToPointer for that sprite. You can do this by adding an onInputDown event to your sprites. Whenever each sprite that has that event is pressed on, the code will run. Something like this:

// Hold a reference to the selected sprite.
selectedSprite;

// Enable the sprite for input so it can be clicked on.
sprite1.inputEnabled = true;
// Add an event to the sprite.
sprite1.events.onInputDown.add(function () {
    // Make sprite1 the current selection.
    selectedSprite = sprite1;
}, this);

sprite2.inputEnabled = true;
sprite2.events.onInputDown.add(function () {
    // Make sprite2 the current selection.
    selectedSprite = sprite2;
}, this);

Then you can do stuff to one sprite at a time, using selectedSprite.

Link to comment
Share on other sites

7 minutes ago, Arcanorum said:

You will need to keep track of which sprite is currently 'selected', and only do moveToPointer for that sprite. You can do this by adding an onInputDown event to your sprites. Whenever each sprite that has that event is pressed on, the code will run. Something like this:


// Hold a reference to the selected sprite.
selectedSprite;

// Enable the sprite for input so it can be clicked on.
sprite1.inputEnabled = true;
// Add an event to the sprite.
sprite1.events.onInputDown.add(function () {
    // Make sprite1 the current selection.
    selectedSprite = sprite1;
}, this);

sprite2.inputEnabled = true;
sprite2.events.onInputDown.add(function () {
    // Make sprite2 the current selection.
    selectedSprite = sprite2;
}, this);

Then you can do stuff to one sprite at a time, using selectedSprite.

It says that selectedSprite is not defined.

Link to comment
Share on other sites

You will need to declare selectedSprite somewhere it can be accessed from the context of the callback function. You can declare it globally by putting var selectedSprite; outside of any functions. Doing this will make it a property of the window object.

I'm assuming you are using the basic game example structure, and not using states.

Link to comment
Share on other sites

23 minutes ago, Arcanorum said:

You will need to declare selectedSprite somewhere it can be accessed from the context of the callback function. You can declare it globally by putting var selectedSprite; outside of any functions. Doing this will make it a property of the window object.

I'm assuming you are using the basic game example structure, and not using states.

Thanks, but for some reason even if i haven't clicked them, they both move towards the mouse click.

(and i'm using states)

Link to comment
Share on other sites

Are you still using the code you had before?

What you were doing before made sense. You were calling moveToPointer for each sprite. Of course they are both going to move.

With the code I suggested before, your update function should look like so.

function update(){
    if(game.input.mousePointer.isDown){
        // Make sure that selectedSprite has something inside it, or moveToPointer
        // will give an error that it can't find the object to move towards.
        if(selectedSprite){
            // Move the selected sprite towards the mouse pointer.
            game.physics.arcade.moveToPointer(selectedSprite, 20);
        }
    }
}

You don't even need that Phaser.Rectangle.contains stuff either, as that is covered by the input events added to the sprites themselves.

Link to comment
Share on other sites

30 minutes ago, Arcanorum said:

Are you still using the code you had before?

What you were doing before made sense. You were calling moveToPointer for each sprite. Of course they are both going to move.

With the code I suggested before, your update function should look like so.


function update(){
    if(game.input.mousePointer.isDown){
        // Make sure that selectedSprite has something inside it, or moveToPointer
        // will give an error that it can't find the object to move towards.
        if(selectedSprite){
            // Move the selected sprite towards the mouse pointer.
            game.physics.arcade.moveToPointer(selectedSprite, 20);
        }
    }
}

You don't even need that Phaser.Rectangle.contains stuff either, as that is covered by the input events added to the sprites themselves.

I can't get it working, only clicks inside sprite2 move it, but i would like clicks anywhere on the screen move it. Also when i click sprite2, sprite1 starts following sprite2, even though i want it to not respond to clicks when it is not selected. I'll just try to find another way to do this.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...