Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

When i run the game particles crash the game, im not sure what am i doing wrong because the program doesnt tell me. But i know particles are causing this , if i remove the code in update code at bottom, the runnable jar works fine.

Particle class : http://pastebin.com/ATLN6qzR

Particle manager : http://pastebin.com/vAk1nwmi

Slick 2d docmentation : http://www.slick2d.org/javadoc/

It says error is here where the for starts

public void update(int delta){

    for (Particle o: particleList) {
        if(o.emitter.completed() == true){
            particleList.remove(o); 
        }
        else{
            o.system.update(delta);
        }

    }
}
share|improve this question

closed as too localized by msell, bummzack, Anko, Byte56, Josh Petrie Apr 18 '13 at 5:00

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Debugging is almost always too localised to be useful to anyone else. Could you condense the problem further than "I don't know what's happening, but here's all my code"? –  Anko Apr 17 '13 at 12:34
1  
i edited the post –  Matthew Apr 17 '13 at 12:52
add comment

1 Answer 1

up vote 4 down vote accepted

I'm not sure how you're not seeing this problem in the debug version.

for (Particle o: particleList) {
    if(o.emitter.completed() == true){
        particleList.remove(o); 
    }

Attempting to remove an item from a list while iterating through that list will give you a concurrency exception.

Instead of particleList.remove(o); add the item to remove list. Then after your for each loop, loop through the remove list and use the particleList.remove(o); call. Then clear your remove list.

Like this:

ArrayList<Particle> toRemove = new ArrayList<Particle>();
for (Particle o: particleList) {
    if(o.emitter.completed() == true)
        toRemove.add(o);
    else
        o.system.update(delta);
}

//remove finished particles
for (Particle o: toRemove) {
    particleList.remove(o);
}
toRemove.clear();
share|improve this answer
    
Yes, this is the problem. Alternatively you can use an Iterator docs.oracle.com/javase/6/docs/api/java/util/Iterator.html –  Karl Apr 17 '13 at 14:32
    
If i understand correctly i should make another list and save the objects that need removing , then i loop trough remove list and compare with with particle list and remove from particle list. –  Matthew Apr 17 '13 at 14:35
    
I've updated the answer with a direct solution. There's no "compare with the particle list". Just remove. –  Byte56 Apr 17 '13 at 14:51
    
thanks , I didn't know it could automaticaly locate the object –  Matthew Apr 17 '13 at 14:56
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.