java - Removing something from array but game crashes -
java - Removing something from array but game crashes -
i have , array create few swords on tiled map , when step on them need deleted when step on them game chrashes.
at java.util.arraylist$itr.checkforcomodification(unknown source) @ java.util.arraylist$itr.next(unknown source)
my code:
i guess loop looking object if deleted?
private list<sword> swords = new arraylist<sword>(); i create them 1 time:
public gamerender(gameworld world, int gameheight) { (int = 0; i<100 ; i++ ) { swords.add(new sword()); } when step on it:
for (sword sword : swords) { if(playery+7>=sword.position.y && playery+7<=sword.position.y+tilesize && playerx+7>=sword.position.x &&playerx+7<=sword.position.x+tilesize) { sword.pickme(); swords.remove(sword); } } there other loop in render():
for (sword sword : swords) { sword.createme(); } i dont think need see sword class. if i'll set in here. how prepare dont error?
for (sword sword : swords) { if(playery+7>=sword.position.y && playery+7<=sword.position.y+tilesize && playerx+7>=sword.position.x &&playerx+7<=sword.position.x+tilesize) { sword.pickme(); swords.remove(sword); //this line throws concurrentmodificationexception } }
due how enhanced for loop works, either need utilize iterator<sword> remove it:
iterator<sword> iterator = swords.iterator(); while(iterator.hasnext()) { sword sword = iterator.next(); if(playery+7>=sword.position.y && playery+7<=sword.position.y+tilesize && playerx+7>=sword.position.x &&playerx+7<=sword.position.x+tilesize) { sword.pickme(); iterator.remove(); } } or following:
(int = 0; < swords.size(); i++) { sword sword = swords.get(i); if(playery+7>=sword.position.y && playery+7<=sword.position.y+tilesize && playerx+7>=sword.position.x &&playerx+7<=sword.position.x+tilesize) { sword.pickme(); swords.remove(i--); } } java arrays libgdx
Comments
Post a Comment