Jump to content

Cannot destroy sprite when onInputUp directly after onInputDown


Politybuilder
 Share

Recommended Posts

I have a situation where there are two sprites - sprite1 and sprite2. sprite1 is enabled for onInputDown as follows:

sprite1.inputEnabled = true;
sprite1.events.onInputDown.add(select, this);

Then, within the select() function, there is some code to work out which sprite to display, which in this case turns out to be sprite2. Once this is done I enable sprite2 for onInputUp:

sprite2 = game.add.image(game.input.mousePointer.x, game.input.mousePointer.y, "sprite2");
sprite2.inputEnabled = true;
sprite2.events.onInputUp.add(deselect, this);

So here, sprite2 appears under (or actually below and to the right of, but never mind) the mouse pointer while the mouse button is still down. Now, if the user lets the button go up, sprite2 should disappear when running deselect()So, if you click on sprite1 then sprite2 will briefly appear and then disappear. But it you click on sprite1 and keep holding the mouse button down then sprite2 will appear and remain there until you let the mouse button go.

function deselect()
{
    sprite2.destroy();
}

BUT this is not what happens dammit.

sprite2 does NOT disappear when you let the mouse button up, it just stays there. However, after this, if you then click on sprite2 and THEN let the mouse button go, it will disappear. How can I make it so that sprite2 appears when the mouse button is held down but disappears if you let it go from the first time the user triggers onInputDown?

Link to comment
Share on other sites

I'm not quite sure I understand, but it looks like you're enabling click events on the sprite itself when you want the sprite to appear or disappear via the mouse regardless of where you click.

You could try checking in your update function whether game.input.activePointer.leftButton.isDown is true or false and adding or destroying the sprite accordingly. 

Link to comment
Share on other sites

I've tried both ways an in each case the result is the same as before - leftButton is undefined. I've also tried it with mousepointer and with different buttons - always the same result - not recognising any of the buttons on the pointer.

Link to comment
Share on other sites

Any given input event (up, down) gets handled by at most one input-enabled object, the topmost one.

Also, sprite2 never gets inputUp because it never received an inputDown in the first place.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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