ack? Why does this code make my game freeze

Also, inside the file I uploaded i made a typo that doesnt really effect freezing, but it still is wrong

if(elementsArray[i][k]!=null)
{
	if(elementsArray[i][k].getElement().equals("ground"))
		groundList[elementsArray[i][k].getCol()] = elementsArray[i][k];
	else if(elementsArray[i][k].isEnemy())
		enemyList[i*levelArray[0].length()+k].add(elementsArray[i][k]);
	else 
		enemyList[i*levelArray[0].length()+k].add(elementsArray[i][k]);
					
		
}

should be replaced with


if(elementsArray[i][k]!=null)
{
	if(elementsArray[i][k].getElement().equals("ground"))
		groundList[elementsArray[i][k].getCol()] = elementsArray[i][k];
	else if(elementsArray[i][k].isEnemy())
		enemyList[i*levelArray[0].length()+k].add(elementsArray[i][k]);
	else 
		itemList[i*levelArray[0].length()+k]=elementsArray[i][k];
}

when you modify a lsit in the middle of a loop over ist indexes you have no gaurantees of what it does to the indexes. Its totally implemntation dependant.

if its working for you, your just gettign lucky.

Use Iterators, thats what they a re for.

http://s53.yousendit.com/d.aspx?id=2NPP2M8HHFAQ03H9KXGMQL3GSJ
^^^ updated file,
[edit[ alrite ill try iterator

http://java.sun.com/j2se/1.5.0/docs/api/java/util/AbstractList.html#remove(int)

“Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).”

So, I’m not lucky. Its the way its (AbstractList ) supposed to work. And yea the indices change, but only of those I already checked.


edit: Duh… Applets… you know that there is lots of caching going on and that you dont necessarly see the changes in effect (if you’re using a browser), right?

Short answer: Use the appletviewer for testing.

i dont understand how to use appletviewer so screw that, but

riven, instead of using s.o.p. i use setBackground(Color.red) to test if a certain place was accessed. and if i have the setBackground(Color.red) in my catch block, then it does change the color.

ok i dont know how to convert everything from a for loop to a iterator

so far


eIterator = eList[enemyIndex].iterator();
while(eIterator.hasNext())
{
									
	if( !quix.getAttackRect().intersects( ( (Enemy) eIterator.next() ).getBounds() ) )
	{
		continue;
	}
									
	if(level.attackElement( enemyIndex,i,50 ))
	{
		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");
       	} 
}

how do i convert this part because i need a particular index;

if(level.attackElement( enemyIndex,i,50 ))
 	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;
	}

ok i tried the iterator but it still freezes


(index count already declared to 0 earlier)

eIterator = eList[enemyIndex].iterator();
while(eIterator.hasNext())
{
	
	if( !quix.getAttackRect().intersects( ( (Enemy) eIterator.next() ).getBounds() ) )
	{
		continue;
	}
	
	if(level.attackElement( enemyIndex,currentIndex,50 ))
	{
			System.out.println("before");
			try{
			currentIndex--;
			eIterator.remove();
			}
			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");
	} 
	currentIndex++;
}
currentIndex=0;

blah i think i fixed it…thanks guys

btw someone tell me how open the applet with an appeltviewer, i cant get it to work

  1. www.google.com
  2. See 3rd result.

You expect help from people when you have that attitude?

Did you read the docs?
What about them didn’t you understand?
Ask some reasonable questions and you will get better help.
(Or you can respond with “screw that” and see how long it is tolerated.)

i did read javadoc along time ago to open a appletviewer
but
i changed my class path to jre instead of jdk (by acc), thats why i couldnt get appletviewer to work until now

Okay, you caught me wrong here 8)
Serves me right for trusting my memory rather thenr efferring to the docs.

Not all collection types however have the same behavior in this regard, of that im sure, so the One Safe Way for all collections is stil lto use an iterator :stuck_out_tongue:

JK