Soljaragz
|
 |
«
Posted
2006-02-05 08:12:17 » |
|
my game freeze whenever an enemy dies.........can someone tell me why? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| 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); i--; setBackground(Color.green); }
} } } } /code] |
1 2 3 4 5 6 7 8 9 10 11 12 13
| 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; } |
|
|
|
|
|
oNyx
|
 |
«
Reply #1 - Posted
2006-02-05 09:13:04 » |
|
1 2 3 4 5
| 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.
|
|
|
|
f.l.x
|
 |
«
Reply #2 - Posted
2006-02-05 12:46:06 » |
|
wow, as Linus Torvalds said "If you need more than 3 levels of indentation, you're screwed anyway, and should fix your program."
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Riven
|
 |
«
Reply #3 - Posted
2006-02-05 13:56:54 » |
|
Indeed, this looks so much better 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| if(quix.facingLeft()) return;
for(int i=eList[ enemyIndex ].size()-1;i>=0;i--) { if(eList[ enemyIndex ].get(i) == null) continue;
if(! quix.getAttackRect().intersects( ( (Enemy) eList[ enemyIndex ].get(i) ).getBounds() ) ) continue;
if(level.attackElement( enemyIndex,i,50 )) { eList[ enemyIndex ].remove(i); i--; setBackground(Color.green); } } |
|
|
|
|
Soljaragz
|
 |
«
Reply #4 - Posted
2006-02-05 19:11:41 » |
|
lol ok i guess i don't really have null values :p and i forgot there was a continuecommand lol
thanks guys let me see what happens
|
|
|
|
|
Soljaragz
|
 |
«
Reply #5 - Posted
2006-02-05 19:20:36 » |
|
edit: [nvm]
|
|
|
|
|
Soljaragz
|
 |
«
Reply #6 - Posted
2006-02-05 19:31:43 » |
|
um lol it still freezes...hmm 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| 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); setBackground(Color.green); } } } } |
|
|
|
|
|
Jeff
|
 |
«
Reply #7 - Posted
2006-02-05 21:02:00 » |
|
Yo ucannot act on the lsit directly from within a loo pthat iterates over it.
Use an iterator and use iterator().remove
|
|
|
|
Soljaragz
|
 |
«
Reply #8 - Posted
2006-02-05 21:06:58 » |
|
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???
|
|
|
|
|
oNyx
|
 |
«
Reply #9 - Posted
2006-02-05 21:32:52 » |
|
Yo ucannot act on the lsit directly from within a loo pthat iterates over it. [...]
You can do that just fine. I'm using that kind of construct (see my previous post) in a couple of places.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Soljaragz
|
 |
«
Reply #10 - Posted
2006-02-05 21:34:24 » |
|
onYx , it still freezes though even when i go backward, check my code
|
|
|
|
|
Riven
|
 |
«
Reply #11 - Posted
2006-02-05 21:55:14 » |
|
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");
|
|
|
|
Soljaragz
|
 |
«
Reply #12 - Posted
2006-02-05 22:29:25 » |
|
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
|
|
|
|
|
oNyx
|
 |
«
Reply #13 - Posted
2006-02-05 22:34:26 » |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| 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: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| 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.  Must be something else.
|
|
|
|
Soljaragz
|
 |
«
Reply #14 - Posted
2006-02-05 22:37:19 » |
|
1 2 3 4 5 6 7 8 9 10 11
| System.out.println("before"); try{ eList[ enemyIndex ].remove(i); } catch(Exception exc) { setBackground(Color.red); exc.printStackTrace(System.out); System.exit(0); } 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
|
|
|
|
|
|
|
Riven
|
 |
«
Reply #16 - Posted
2006-02-06 00:05:20 » |
|
1 2 3 4 5 6 7 8 9 10 11
| System.out.println("before"); try{ eList[ enemyIndex ].remove(i); } catch(Exception exc) { setBackground(Color.red); exc.printStackTrace(System.out); System.exit(0); } System.out.println("after"); |
didn't work right. What kind of useless response is that. At least tell what happened.
|
|
|
|
Soljaragz
|
 |
«
Reply #17 - Posted
2006-02-06 00:24:03 » |
|
sry... lol the thing is NOTHING HAPPENED LOL, it just froze and thats it
|
|
|
|
|
Riven
|
 |
«
Reply #18 - Posted
2006-02-06 00:40:28 » |
|
So you didn't see either "before" or "after" printed? Right... as still you're telling me the call to remove() is freezing...
|
|
|
|
Soljaragz
|
 |
«
Reply #19 - Posted
2006-02-06 00:47:18 » |
|
no i didnt see either, system.out.println works for applets too?
|
|
|
|
|
Soljaragz
|
 |
«
Reply #20 - Posted
2006-02-06 00:51:02 » |
|
Also, inside the file I uploaded i made a typo that doesnt really effect freezing, but it still is wrong 1 2 3 4 5 6 7 8 9 10 11
| 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 1 2 3 4 5 6 7 8 9
| 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]; } |
|
|
|
|
|
Jeff
|
 |
«
Reply #21 - Posted
2006-02-06 00:59:08 » |
|
Yo ucannot act on the lsit directly from within a loo pthat iterates over it. [...]
You can do that just fine. I'm using that kind of construct (see my previous post) in a couple of places. 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.
|
|
|
|
Soljaragz
|
 |
«
Reply #22 - Posted
2006-02-06 01:03:17 » |
|
|
|
|
|
|
oNyx
|
 |
«
Reply #23 - Posted
2006-02-06 01:12:26 » |
|
Yo ucannot act on the lsit directly from within a loo pthat iterates over it. [...]
You can do that just fine. I'm using that kind of construct (see my previous post) in a couple of places. 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://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.
|
|
|
|
Soljaragz
|
 |
«
Reply #24 - Posted
2006-02-06 01:36:44 » |
|
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.
|
|
|
|
|
Soljaragz
|
 |
«
Reply #25 - Posted
2006-02-06 01:42:00 » |
|
ok i dont know how to convert everything from a for loop to a iterator so far 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| 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); } System.out.println("after"); } } |
how do i convert this part because i need a particular index; 1
| if(level.attackElement( enemyIndex,i,50 )) |
1 2 3 4 5 6 7 8 9 10 11 12 13
| 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; } |
|
|
|
|
|
Soljaragz
|
 |
«
Reply #26 - Posted
2006-02-06 01:49:36 » |
|
ok i tried the iterator but it still freezes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| (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); } System.out.println("after"); } currentIndex++; } currentIndex=0; |
|
|
|
|
|
Soljaragz
|
 |
«
Reply #27 - Posted
2006-02-06 02:17:14 » |
|
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
|
|
|
|
|
|
|
swpalmer
|
 |
«
Reply #29 - Posted
2006-02-07 03:48:32 » |
|
i dont understand how to use appletviewer so screw that, ...
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.)
|
|
|
|
|