ack? Why does this code make my game freeze

my game freeze whenever an enemy dies…can someone tell me why?

enemyIndex = row*level.getLevelWidth()+quix.getCol();
			
			
if(!quix.facingLeft())
{
	for(int i=0;i<eList[ enemyIndex ].size();i++)
	{
		if(eList[ enemyIndex ].get(i) != null)
		{
			if( quix.getAttackRect().intersects( ( (Enemy) eList[ enemyIndex ].get(i) ).getBounds() ) )
			{
				if(level.attackElement( enemyIndex,i,50 ))
				{	
					eList[ enemyIndex ].remove(i); //THIS SPECIFICALLY FREEZES THE GAME
					i--;
					setBackground(Color.green);
							
			       }	
							

		      }
	     }
						
					
      }
					
}
/code]

public boolean attackElement(int arrayIndex, int listIndex, int damage)
	{
		
		((Enemy)enemyList[ arrayIndex ].get(listIndex) ).attack(damage);
		if( ( (Enemy)enemyList[ arrayIndex].get(listIndex) ).getHp()<=0)
		{
			elementsArray[arrayIndex/getLevelWidth()][arrayIndex%getLevelWidth()] = null;
			enemyList[arrayIndex].remove(listIndex);
			return true;
		}
		else
			return false;
	}
for(int i=al.size()-1;i>=0;--i) {
	Thingy t=(Thingy)al.get(i);
	if(t.isDead())
		al.remove(i);
}

Walk backwards through it like that. Like if you remove element 2, the elements 1 and 0 will stay at their place.

wow, as Linus Torvalds said

[quote]“If you need more than 3 levels of indentation, you’re screwed anyway, and should fix your program.”
[/quote]

Indeed, this looks so much better

if(quix.facingLeft())
return;

for(int i=eList[ enemyIndex ].size()-1;i>=0;i--)
{
	if(eList[ enemyIndex ].get(i) == null) // do you really have null values here?
	   continue;

	if(! quix.getAttackRect().intersects( ( (Enemy) eList[ enemyIndex ].get(i) ).getBounds() ) )
            continue;

	if(level.attackElement( enemyIndex,i,50 ))
	{	
	   eList[ enemyIndex ].remove(i); //THIS SPECIFICALLY FREEZES THE GAME
	   i--;
	   setBackground(Color.green);			
       }		
}

lol ok i guess i don’t really have null values :stuck_out_tongue:
and i forgot there was a continuecommand lol

thanks guys let me see what happens

edit: [nvm]

um lol it still freezes…hmm

	for(int row=0;row<4;row++)
	   {
		enemyIndex = row*level.getLevelWidth()+quix.getCol();
							
		if(!quix.facingLeft())
		{
			for(int i=eList[ enemyIndex ].size()-1;i>=0;i--)
			{
									
				if( !quix.getAttackRect().intersects( ( (Enemy) eList[ enemyIndex ].get(i) ).getBounds() ) )
				{
					continue;
				}
									
				if(level.attackElement( enemyIndex,i,50 ))
				{
  					eList[ enemyIndex ].remove(i); //THIS SPECIFICALLY FREEZES THE GAME
   										
   					setBackground(Color.green);
       				} 
			}
                 }
									
           } 

Yo ucannot act on the lsit directly from within a loo pthat iterates over it.

Use an iterator and use iterator().remove

why can’t I???

it removes it from the list, that would make sense so why wouldnt that work?

and if i use iterator I would i have to use listiterator? cause I have to go backwards right???

You can do that just fine. I’m using that kind of construct (see my previous post) in a couple of places.

onYx , it still freezes though even when i go backward, check my code

remove(int) just can’t freeze

There just is nothing as a freeze in Java for this situation.
You might have an uncaught Exception that’s going up the stack and there the handlings makes it block, or it’s not caught anywhere at all, the current thread will just die.

Try this;
System.out.println(“before”);
try{
remove(i);
}
catch(Exception exc)
{
exc.printStackTrace(System.out);
System.exit(0); // makes sure this is the last data that turns up in the output
}
System.out.println(“after”);

yeh i tried that already, and it didn’t freeze but then

WOULDNT IT MEAN THAT THE LINE
“remove(i)” DIDNT WORK?

why would i have to try and catch it? it should just work without it

import java.util.*;

public class Remove{
	public static void main(String[]args){
		ArrayList al=new ArrayList();
		System.out.println("filling with 0-9");

		for(int i=0;i<10;i++)
			al.add(new Integer(i));

		System.out.println("removing all even numbers");
		for(int i=al.size()-1;i>=0;--i)
			if(((Integer)al.get(i)).intValue()%2==0)
				al.remove(i);

		System.out.println("looks like this now:");
		for(int i=0;i<al.size();i++)
			System.out.println((Integer)al.get(i));

		al.clear();

		System.out.println("filling with 0-9");
		for(int i=0;i<10;i++)
			al.add(new Integer(i));

		System.out.println("removing all numbers which are bigger than 5");
		for(int i=al.size()-1;i>=0;--i)
			if(((Integer)al.get(i)).intValue()>5)
				al.remove(i);

		System.out.println("looks like this now:");
		for(int i=0;i<al.size();i++)
			System.out.println((Integer)al.get(i));

		al.clear();

		System.out.println("filling with 0-9");
		for(int i=0;i<10;i++)
			al.add(new Integer(i));

		System.out.println("removing all numbers which are smaller than 5");
		for(int i=al.size()-1;i>=0;--i)
			if(((Integer)al.get(i)).intValue()<5)
				al.remove(i);

		System.out.println("looks like this now:");
		for(int i=0;i<al.size();i++)
			System.out.println((Integer)al.get(i));

	}
}

output:

filling with 0-9
removing all even numbers
looks like this now:
1
3
5
7
9
filling with 0-9
removing all numbers which are bigger than 5
looks like this now:
0
1
2
3
4
5
filling with 0-9
removing all numbers which are smaller than 5
looks like this now:
5
6
7
8
9

See? Works as advertised. :slight_smile:

Must be something else.

System.out.println("before");
											try{
											eList[ enemyIndex ].remove(i);
											}
											catch(Exception exc)
											{
											setBackground(Color.red);
											exc.printStackTrace(System.out);
											System.exit(0); // makes sure this is the last data that turns up in the output
											}
											System.out.println("after");

didn’t work right.

but onxy, when i take out "eList[ enemyIndex ].remove(i); " it doesnt freeze, and every other part of prog dealing with it makes sense imo…

would somebody want to read my entire prog??? cause apparently nothings working

THis is my entire game folder, can someone plz take a look at it because Im desperate =( and it freezes when i kill something no matter what i try
http://s65.yousendit.com/d.aspx?id=3J0ZCLDG92C0W0FVSXN5274MGU

What kind of useless response is that. At least tell what happened.

sry…
lol the thing is NOTHING HAPPENED LOL, it just froze and thats it

So you didn’t see either “before” or “after” printed? Right… as still you’re telling me the call to remove() is freezing…

no i didnt see either, system.out.println works for applets too?