Politybuilder Posted May 2, 2017 Share Posted May 2, 2017 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 More sharing options...
RubbleGames Posted May 2, 2017 Share Posted May 2, 2017 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 More sharing options...
Politybuilder Posted May 2, 2017 Author Share Posted May 2, 2017 I tried that but game.input.activePointer.leftButton.isDown is giving me Cannot read property 'isDown' of undefined. Link to comment Share on other sites More sharing options...
RubbleGames Posted May 2, 2017 Share Posted May 2, 2017 Sorry, you'll need to capture the input first in your create function eg game.input.activePointer.capture = true; There's more than one way to do it, have a look at the examples such as https://phaser.io/examples/v2/input/mouse-buttons Link to comment Share on other sites More sharing options...
Politybuilder Posted May 3, 2017 Author Share Posted May 3, 2017 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 More sharing options...
samme Posted May 3, 2017 Share Posted May 3, 2017 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 More sharing options...
Politybuilder Posted May 3, 2017 Author Share Posted May 3, 2017 Ah I wondered if that was it. I'm thinking of doing this differently - creating a transparent sprite1 and then changing it to something from a spritesheet when it gets clicked on. This way all the clicking is done on the same sprite. Link to comment Share on other sites More sharing options...
Recommended Posts