1

This is the line I'm facing with, I want to detect mouse click when being used in PC (which is working completey fine) and to detect clicks when used in mobile too.

if (keyDown("left") || mousePressedOver(left)) {
  sofia.velocityX = -1
  sofiavelocityY = 0
}

Note: I'm using p5.js, p5.play.js & p5.dom.min.js

I tried several other mouse click functions such as mouseOver etc.

Aaron Meese
  • 1,670
  • 3
  • 22
  • 32
Drew
  • 11
  • 1
  • I cannot find `mousePressedOver` in the P5.js documentation, but if you want to detect clicks use [mouseClicked()](https://p5js.org/reference/#/p5/mouseClicked). Not related to the question, but the line `sofiavelocityY = 0` should be `sofia.velocityY = 0` – Sembei Norimaki Jun 05 '23 at 11:29
  • I did tried mouseClicked before, but I have found the fix I have created an invisible sprite with it's X and Y position set as mouseX and mouseY and then I have used isTouching function to detect the collision ,btw sofiavelocity = 0, was mistake we found out earlier, but didn't corrected it intentionally, cauz this made the game much more hard to complete. – Drew Jun 07 '23 at 06:51
  • if you don't want to set the y velocity to 0, then comment or delete the line. but what are you doing in your code is: assigning -1 to the attribute `velocityX` of the instance `sofia` and in the next line you are assigning 0 to a variable `sofiavelocityY` which is a completely different thing. You are mixing apples and oranges here which you shouldn't. Either use `sofia.velocityY = 0` or delete the line. – Sembei Norimaki Jun 07 '23 at 07:42
  • Then about your solution, I don't think creating an invisible sprite in mouseX, mouseY and checking if this sprite collides with the target one will work (it will only work for circles, but the moment the sprite has soma asymmetry this will cause false positives for some regions and false negatives for other regions). You can use [mouseOver()](https://p5js.org/reference/#/p5.Element/mouseOver) for that – Sembei Norimaki Jun 07 '23 at 07:48

1 Answers1

1

If you want to check for clicking a sprite with the mouse, use the latest version of p5play (v3).

if (sofia.mouse.pressing()) {
  sofia.velocity.x = -1
  sofia.velocity.y = 0
}

See the input devices page for a full example

quinton-ashley
  • 133
  • 2
  • 10