I’m trying to make a piece of code that displays the co-ordinates at which the mouseX and mouseY co-ordinates are. This is the code I have so far… Help?
int mousex = mouseX;
int mousey = mouseY;
void setup ()
{
size (800,500);
if (mousePressed == true)
println ("vertex (",mousex,mousey,")");
}
You need to execute the println(); part inside the draw() function. The setup() method is only executed once at the start of the sketch, so it will only show the coordinates once at the start, where mouseX and Y are still 0.
To add to your solution, a “better” way of doing this would be to use the mousePressed() function.
The mousePressed variable check the real time state of the mouse button. The problem having it in draw like you did is that the draw loop will be executed 60 times per second and everytime it will check the state of the mouse button and print something if it is pressed. So if you press the button for a bit too long you’ll spam the console.
I can see that you figured out that problem since you limited the frame rate at 10 image per second (by the way, this is the kind of function that you want to put in setup(), no need to call it every frame). The problem with that method is that you decrease the reactivity of your program:
Image that you have other code that needs to be executed like draw a moving circle to the screen. In this case you’ll draw the circle only 10 times per second so the animation will appear “buggy”.
The other effect that you can witness with your current code is that when you click the mouse, the result does not appear instantly. And worse, since it is checking the real time state of the mouse, if you press your mouse button when the code is not executed, nothing happen.
Now the mousePressed() function solve that problem. It is getting executed after the mouse button get down. So not when the mouse button is down but when it goes from being up the being down. This way, you can keep your frame rate as high as you want and the message will be displayed only once.
And to add to that, my solution (to check mousePressed in draw every frame) is better for contiuous checks. Same is valid for keyPressed. Just adding it, because i had a lot of problems with having to click thousand times to get to a desired variable (with buttons that in/decrease a value). If the mouse should be updated every time it moves and is pressed, then the mouseDragged() function would be better.