java - Listiterator in Android Game -
i have 2 arraylist's using, 1 enemy sprites , other bullets. when run app crashes on emulator , within few levels crashes on device upon collision. log keeps telling me have simple listiterator error. wanted know how implement listiterator. have not been using listiterator, searched around find information , examples bit confused on how it. add() function called in ontouch method of view class , collision takes place inside of thread class in own collision methods.
log:
07-20 19:57:46.604: error/androidruntime(234): uncaught handler: thread thread-9 exiting due uncaught exception 07-20 19:57:46.613: error/androidruntime(234): java.util.concurrentmodificationexception 07-20 19:57:46.613: error/androidruntime(234): @ java.util.abstractlist$simplelistiterator.next(abstractlist.java:64) 07-20 19:57:46.613: error/androidruntime(234): @ com.android.hitmanassault.hitmanview$hitmanthread.startgame(hitmanview.java:333) 07-20 19:57:46.613: error/androidruntime(234): @ com.android.hitmanassault.hitmanview$hitmanthread.gamestart(hitmanview.java:290) 07-20 19:57:46.613: error/androidruntime(234): @ com.android.hitmanassault.hitmanview$hitmanthread.updategame(hitmanview.java:393) 07-20 19:57:46.613: error/androidruntime(234): @ com.android.hitmanassault.hitmanview$hitmanthread.run(hitmanview.java:237)
checks collision:
private void startgame(){ synchronized(msurfaceholder){ for(beam bullet: beam){ for(sprite sprite: sprites){ if(checkcollision(sprite, bullet)){ sprites.remove(sprite); beam.remove(bullet); mscore = mscore + 1; break; } } } } }
collision method:
public boolean checkcollision(sprite sprite, beam bullet){ boolean retvalue = false; int spritex = sprite.getx(); int spritey = sprite.gety(); int spritexs = sprite.getx() + sprite.getwidth(); int spriteys = sprite.gety() + sprite.getheight(); int beamx = bullet.getx(); int beamy = bullet.gety(); int beamxs = bullet.getx() + bullet.getbitmap().getwidth(); int beamys = bullet.gety() + bullet.getbitmap().getheight(); if ((beamx >= spritex && beamx <= spritexs) || (beamxs >= spritex && beamxs <= spritexs)) { if ((beamy >= spritey && beamy <= spriteys) || (beamys >= spritey && beamys <= spriteys)) { retvalue = true; } } return retvalue; }
if add element list while thread iterating list, concurrentmodificationexception.
you can try concurrentlinkedqueue, depending on requirement on consistency model of game, should rethink architecture.
this problem.
for(sprite sprite: sprites) { <------- iterate if(checkcollision(sprite, bullet)) { sprites.remove(sprite); <-------- modify ...
quick fix gather element want remove remove them after loop.
arraylist<beam> toberemovebeams = new arraylist<beam>(); arraylist<sprite> toberemovesprites = new arraylist<sprite>(); for(beam bullet: beam){ for(sprite sprite: sprites){ if(checkcollision(sprite, bullet)){ toberemovebeams.add(beam); toberemovesprites.add(sprite); mscore = mscore + 1; break; } } } beam.removeall(toberemovebeams); sprites.removeall(toberemovesprites );
Comments
Post a Comment