Player should move on drag, shouldn’t respond on click

Above is the link of my code in p5.js editor.
As you can see in the code that the player which is in blue colour on the preview screen, moves on mouseX and mouseY.
I just need that player to move on dragging, it shouldn’t respond if I click over an empty region.
Can someone please help me…

1 Like


Please follow the guidelines on how to post on this forum : :slight_smile:

If you want to drag your player, you need three steps :

  • Detect if the mouse is pressed and inside the player image (you can use basic if statements)

  • Use the function mouseDragged() to detect when the user is dragging the mouse

  • Update the player’s position, you might want to use pmouseX and pmouseY to get the difference between two mouse positions


I need to disable the movement of the player if I click anywhere over the screen.

I don’t understand.

If you implement what I’ve said before, if you drag your mouse on your player then you are going to move it. Otherwise, if it’s anywhere on the screen you do nothing.

It isn’t working in my code…you can also try

You can check the distance from the mouse to the player using if with dist() command - see reference

1 Like