We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi Karsten and everybody!
I am trying to use Toxilibs with this example: http://www.openprocessing.org/sketch/17191
Where can i find an example about how to remove particles of a system? I am having an error trying the removeParticle method.
For example, to make a system that grows in particles until a limit and after that, kill them.
I really appreciate your help.
Thanks in advance and warm regards.
Patricio
Comments
What is the error you are getting?
Hi Colour! Im having a java.util.ConcurrentModificationException error.
The code i am trying (look at mousePressed function):
Ahh, that's an annoying error. It means that there are multiple threads, one is displaying the particles, the other is removing a particle. Both happen at the same time, so a particle is removed when Processing tries to display it. This gives the error. I'm not entirely sure what the best way to handle this is, but my solution is to add a boolean that turns true when the removeParticle() is activated.
I can't make much of your code (please format it with ctrl+t in Processing before pasting it in the forum and then select the code and press the C button), but this is the idea:
I hope this solves it.
Thanks a lot for your help and sorry for the wrong copy&paste.
I tried your code but im still having the error. Here is the code
Oh! man sorry again!!!
I don't think this is a threading issue; rather, you're trying to iterate over an ArrayList (I assume?) while removing items from it. You might be able to avoid it by using a for loop iterating in reverse, e.g. something like this (untested) :
I'm out of suggestions now... maybe also include the if(!removing) inside the addParticle() method? But if that doesn't solve it, I hope one of the experts sees this. Maybe something else is causing the error, but I don't see what.
Edit, or what velvet said. Isn't there a better way to remove all particles, like physics.particles.clear()? Not sure about the syntax, you might want to look into the documentation.
You are getting the error because you are removing elements from the
ArrayList
while looping through it. This is a big no in Java. There are ways of getting around this, however.If you want to remove all items from an
ArrayList
, you can useArrayList.clear()
. So yourmousePressed()
function would be:If you want to remove individual items from an
ArrayList
, you should use anIterator
. I won't provide an example because this isn't necessarily what the OP asked... but it's worth mentioning.