Using mouse in p5 is a bit complicated

tell me of anything that I can use that is executed in only one frame when mouse is pressed within the if condition…

plz I wan it fast… Just like mousePressed(); but i want to use in an if condition

https://p5js.org/reference/#/p5/mouseIsPressed

@SomeOne that is the rpoblem, mouseIsPressed is true till you press…

huh, did you read the description?

Description

The boolean system variable mouseIsPressed is true if the mouse is pressed and false if not.

I know that… I am just saying, if I write something like this

if(mouseIsPressed){
playState ++;
}

It should only get one above, I’ll use mouseIsPressed and it’ll go I don’t know how many places above

Maybe you want this instead?

function mouseClicked() {
  playState++;
}
3 Likes

I see … yeah you should use one of the event listeners, mousePressed() or mouseClicked(). mouseIsPressed() is a boolean function that you are probably calling in draw() so it’s getting fired every frame until you release the mouse. The time it takes to do a natural mouse click takes several frames …

2 Likes

the way I’ve done it, i’ve put a delay between two mouse-click conditions. Its quite working thank you anyways

@slow_izzm and plz help me with it, i’m using buffers for drawing an image and using it for my sprite. it’s quite done, but at last I want to change my background. when I do it, the images are gone. Is this a bug? If yes, any alternative to it??

It’s not a bug. Most common pattern in processing is

def draw() {
  background(0);
  //draw things again and again

Because erasing or removing a graphical element is difficult or impossible we use repeatedly called draw()-function and start by background() to clear canvas and draw all elements again. Because draw is called 60 times a second you can create elaborate animations by changing what is drawn by each round of draw() calls.

2 Likes

but i used buffers, background should not interfere in it…

background clears canvas, the visible drawing area. And in the same manner it can clear your buffers

buffer.background(0);

would set buffer to black and clear everything on it. You can use the same draw() cycle with buffers too

1 Like

that is something I don’t want to do, it’s happening on its own

What do you mean?? Background happens on it’s own or draw() cycle goes on on it’s own. They are both built that way. You can prevent draw() from happening with noLoop()-function, but background() clears canvas and that’s it.

1 Like

Can you share your code so we can take a look?

1 Like

it is really hard without a code :rofl::rofl::rofl:… ANYWAYS, i have got a better plan about it… @slow_izzm @SomeOne. THANKS FOR RESPONDING…