I am using a canvas, moving objects on the screen, when an object hit the left side of the canvas (x=0), another object of the same type gets instantiated, and start moving on the screen.
Everything works fine, a few objects gets created and start moving around the screen.
At a certain point, I receive a concurrent modification exception in my run method where the game loop is, where gameObjs is an ArrayList:
@Override
public void run() {
while(isRunning){
if(!myHolder.getSurface().isValid())
continue;
Canvas canvas = myHolder.lockCanvas();
canvas.drawRect(0,0,canvas.getWidth(), canvas.getHeight(), pWhite);
for(MyGameObject gameObj : gameObjs){
gameObj.move(canvas);
}
myHolder.unlockCanvasAndPost(canvas);
}
}
I've tried to use an Iterator, but still getting the same error.
I really appreciate your help. Thank you in advance!
gameObjs
? (i.e. anArrayList
?) Is another thread running that is adding / removing items in this collection? If this is happening and it is allowed, you could use aCopyOnWriteArrayList
or some other collection that allows concurrent modification...