Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  ack? Why does this code make my game freeze  (Read 3567 times)
0 Members and 1 Guest are viewing this topic.
Offline Soljaragz

Junior Member





« Posted 2006-02-05 07: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); //THIS SPECIFICALLY FREEZES THE GAME
              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;
   }

Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #1 - Posted 2006-02-05 08: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.

弾幕 ☆ @mahonnaiseblog
Offline f.l.x

Senior Member


Projects: 3


there is no place like 127.0.0.1


« Reply #2 - Posted 2006-02-05 11:46:06 »

wow, as Linus Torvalds said
Quote
"If you need more than 3 levels of indentation, you're screwed anyway, and should fix your program."

Litterarum radices amaras, fructus dulces
http://flx.proyectoanonimo.com
figth spam!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 800
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2006-02-05 12: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) // 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);        
       }      
}

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Soljaragz

Junior Member





« Reply #4 - Posted 2006-02-05 18: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
Offline Soljaragz

Junior Member





« Reply #5 - Posted 2006-02-05 18:20:36 »

edit: [nvm]
Offline Soljaragz

Junior Member





« Reply #6 - Posted 2006-02-05 18: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); //THIS SPECIFICALLY FREEZES THE GAME
                               
                  setBackground(Color.green);
                   }
         }
                 }
                           
           }
Offline Jeff

JGO Coder




Got any cats?


« Reply #7 - Posted 2006-02-05 20:02:00 »

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

Use an iterator and use iterator().remove

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Soljaragz

Junior Member





« Reply #8 - Posted 2006-02-05 20:06:58 »

why can't I?HuhHuh


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???
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #9 - Posted 2006-02-05 20: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.

弾幕 ☆ @mahonnaiseblog
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Soljaragz

Junior Member





« Reply #10 - Posted 2006-02-05 20:34:24 »

onYx , it still freezes though even when i go  backward, check my code
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 800
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #11 - Posted 2006-02-05 20: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
Offline Soljaragz

Junior Member





« Reply #12 - Posted 2006-02-05 21: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
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #13 - Posted 2006-02-05 21: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. Smiley

Must be something else.

弾幕 ☆ @mahonnaiseblog
Offline Soljaragz

Junior Member





« Reply #14 - Posted 2006-02-05 21: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); // 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
Offline Soljaragz

Junior Member





« Reply #15 - Posted 2006-02-05 23:02:39 »

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
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 800
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #16 - Posted 2006-02-05 23: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); // makes sure this is the last data that turns up in the output
                                }
                                 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
Offline Soljaragz

Junior Member





« Reply #17 - Posted 2006-02-05 23:24:03 »

sry...
lol the thing is NOTHING HAPPENED LOL, it just froze and thats it
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 800
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #18 - Posted 2006-02-05 23: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
Offline Soljaragz

Junior Member





« Reply #19 - Posted 2006-02-05 23:47:18 »

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

Junior Member





« Reply #20 - Posted 2006-02-05 23: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];
}
Offline Jeff

JGO Coder




Got any cats?


« Reply #21 - Posted 2006-02-05 23: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.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Soljaragz

Junior Member





« Reply #22 - Posted 2006-02-06 00:03:17 »

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

JGO Coder


Medals: 2


pixels! :x


« Reply #23 - Posted 2006-02-06 00: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.

弾幕 ☆ @mahonnaiseblog
Offline Soljaragz

Junior Member





« Reply #24 - Posted 2006-02-06 00: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.
Offline Soljaragz

Junior Member





« Reply #25 - Posted 2006-02-06 00: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); // 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;
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;
   }
Offline Soljaragz

Junior Member





« Reply #26 - Posted 2006-02-06 00: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); // makes sure this is the last data that turns up in the output
        }
         System.out.println("after");
   }
   currentIndex++;
}
currentIndex=0;

Offline Soljaragz

Junior Member





« Reply #27 - Posted 2006-02-06 01: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
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 800
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #28 - Posted 2006-02-06 08:56:25 »

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

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #29 - Posted 2006-02-07 02: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.)

Pages: [1] 2
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Grunnt (4 views)
2014-09-23 14:38:19

radar3301 (13 views)
2014-09-21 23:33:17

BurntPizza (31 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (29 views)
2014-09-20 20:14:06

BurntPizza (33 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (75 views)
2014-09-10 13:57:51

TehJavaDev (104 views)
2014-09-10 06:39:09
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!