While statement causing freeze/crash in LibGDX? -


i have following code in update method of base level class:

while(entities.iterator().hasnext()){         if(entities.iterator().next() != null){             entities.iterator().next().update();             gdx.app.log(game.log, "updated entity "+entities.iterator().next().getname()+".");         }         else{             gdx.app.log(game.log, "could not update entity.");         }     } 

however, statement freeze program when run, , have force closed without providing crash information. can stop freezing using if statement instead of while, however, update first entity in array.

what causing freeze, , how can iterator looped without causing it?

don't call iterator() , next() methods more required. iterator() method reset iterator on every call. next() method fetch next item on every call. instead use this:

iterator<t> iterator = entities.iterator(); while(iterator.hasnext()) {     t entity = iterator.next();     entity.update(); } 

where t should replaced class of entity.

edit, easier using syntactic sugar:

for (t entity : entities) {     entity.update(); } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -