We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi guys, So I know in processing there is a function called mouseClicked() that will execute the code inside it when the mouse is clicked. However the more objects I have in my program, the laggier and slower the mouse click detection becomes. Sometimes nothing happens when I click. How do i fix this? (I'm using eclipse IDE) Here is my code:
public void mouseClicked() {
if (room.equals("game")) {
for (Group group : discoveredGroups) {
group.groupClicked();
}
for (Group groupSelected : groupSelectedList) {
if (groupSelected != null) {
groupSelected.groupClicked();
}
}
for (Button button : buttons) {
button.buttonClicked();
}
for (Arrow arrow : arrows) {
arrow.arrowClicked();
}
for (Pane pane : panes) {
pane.paneClicked();
}
// avoid concurrent modification error
if (elementClickPause == false) {
for (Element element : elementListA) {
if (element != null) {
element.elementClicked();
}
}
for (Element element : elementListB) {
if (element != null) {
element.elementClicked();
}
}
}
} else if (room.equals("menu")) {
for (Button button : buttons) {
button.buttonClicked();
}
if (drawPackList == true) {
for (Pack pack : packObjects) {
pack.packClicked();
}
}
for (Arrow arrow : arrows) {
arrow.draw();
}
for (Pane pane : panes) {
pane.paneClicked();
}
} else {
for (Button button : buttons) {
button.buttonClicked();
}
}
Answers
Is your mouse event laggish or more like your program struggling to keep up with your computations? If the frameRate drops, I will guess that mouse events will not be processed orderly and maybe even some of those events missed...
I would be interested to know if you notice any lagging in your actual code. Can you tell?
If you do this in draw():
if(frameCount%60==0) {println("Current FR: "+frameRate());}
what values do you get on average?Kf
@kfrajer around 60
mouseClicked(), like all the other user input callbacks, are synchronous in Processing.
The screen is uploaded only when draw() + all trigger functions are fully processed! :-<
Processing offers thread() so you can delegate a separate Thread to do a job: :-bd
https://Processing.org/reference/thread_.html
However, you're gonna need to protect that whole threaded job, given your iterating over lotsa diff. containers. :-SS
Any other place which changes the size() of any of those containers gotta be protected by
synchronized () {}
blocks as well, under the same monitor Object reference. =;@GoToLoop The OP is reporting 60 fps... then what could be causing the lagging behavior? Not sure if a separate thread would help here, or would it?
Kf
It sounds like OP is calling an arbitrarily complex collision detection function for a growing list of (dozens, hundreds, thousands?) of objects each time mouseClicked fires...?
If so, clicks are trying to run a LOT of code -- optimization might also be necessary here. For example, if your objects are small and collision detection is expensive you could keep objects in region bins (e.g. 9 bins, 16 bins) to speed lookup. Or approximate the collision detection. Or do two-pass or multi-pass collision detection to find candidates cheaply, then only test those candidates.