Soljaragz
Jr. Member   Posts: 55
|
 |
«
on:
2006-02-05 02: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
JGO Kernel      Posts: 2943 Medals: 5
pixels! :x
|
 |
«
Reply #1 on:
2006-02-05 03: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
Sr. Member   Posts: 305
there is no place like 127.0.0.1
|
 |
«
Reply #2 on:
2006-02-05 06: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! Go get 'em!
|
|
Riven
« League of Dukes » JGO Kernel      Posts: 5866 Medals: 255
Hand over your head.
|
 |
«
Reply #3 on:
2006-02-05 07: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); } } |
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings
|
|
|
Soljaragz
Jr. Member   Posts: 55
|
 |
«
Reply #4 on:
2006-02-05 13: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
Jr. Member   Posts: 55
|
 |
«
Reply #5 on:
2006-02-05 13:20:36 » |
|
edit: [nvm]
|
|
|
|
|
Soljaragz
Jr. Member   Posts: 55
|
 |
«
Reply #6 on:
2006-02-05 13: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 on:
2006-02-05 15: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
Jr. Member   Posts: 55
|
 |
«
Reply #8 on:
2006-02-05 15: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
JGO Kernel      Posts: 2943 Medals: 5
pixels! :x
|
 |
«
Reply #9 on:
2006-02-05 15: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! Go get 'em!
|
|
Soljaragz
Jr. Member   Posts: 55
|
 |
«
Reply #10 on:
2006-02-05 15:34:24 » |
|
onYx , it still freezes though even when i go backward, check my code
|
|
|
|
|
Riven
« League of Dukes » JGO Kernel      Posts: 5866 Medals: 255
Hand over your head.
|
 |
«
Reply #11 on:
2006-02-05 15: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");
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings
|
|
|
Soljaragz
Jr. Member   Posts: 55
|
 |
«
Reply #12 on:
2006-02-05 16: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
JGO Kernel      Posts: 2943 Medals: 5
pixels! :x
|
 |
«
Reply #13 on:
2006-02-05 16: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
Jr. Member   Posts: 55
|
 |
«
Reply #14 on:
2006-02-05 16: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
« League of Dukes » JGO Kernel      Posts: 5866 Medals: 255
Hand over your head.
|
 |
«
Reply #16 on:
2006-02-05 18: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.
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings
|
|
|
Soljaragz
Jr. Member   Posts: 55
|
 |
«
Reply #17 on:
2006-02-05 18:24:03 » |
|
sry... lol the thing is NOTHING HAPPENED LOL, it just froze and thats it
|
|
|
|
|
Riven
« League of Dukes » JGO Kernel      Posts: 5866 Medals: 255
Hand over your head.
|
 |
«
Reply #18 on:
2006-02-05 18: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...
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings
|
|
|
Soljaragz
Jr. Member   Posts: 55
|
 |
«
Reply #19 on:
2006-02-05 18:47:18 » |
|
no i didnt see either, system.out.println works for applets too?
|
|
|
|
|
Soljaragz
Jr. Member   Posts: 55
|
 |
«
Reply #20 on:
2006-02-05 18: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 on:
2006-02-05 18: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
Jr. Member   Posts: 55
|
 |
«
Reply #22 on:
2006-02-05 19:03:17 » |
|
|
|
|
|
|
oNyx
JGO Kernel      Posts: 2943 Medals: 5
pixels! :x
|
 |
«
Reply #23 on:
2006-02-05 19: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
Jr. Member   Posts: 55
|
 |
«
Reply #24 on:
2006-02-05 19: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
Jr. Member   Posts: 55
|
 |
«
Reply #25 on:
2006-02-05 19: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
Jr. Member   Posts: 55
|
 |
«
Reply #26 on:
2006-02-05 19: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
Jr. Member   Posts: 55
|
 |
«
Reply #27 on:
2006-02-05 20: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
|
|
|
|
|
Riven
« League of Dukes » JGO Kernel      Posts: 5866 Medals: 255
Hand over your head.
|
 |
«
Reply #28 on:
2006-02-06 03:56:25 » |
|
1. www.google.com2. See 3rd result.
|
Hi, appreciate more people! Σ ♥ = ¾ Learn how to award medals... and work your way up the social rankings
|
|
|
swpalmer
JGO Kernel      Posts: 3438 Medals: 4
Where's the Kaboom?
|
 |
«
Reply #29 on:
2006-02-06 21: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.)
|
|
|
|
|