问题
I am trying to develop an application and i want the sprites to erase once they reach the end. I am using arraylist to handle my sprites on a canvas. I want the sprites to erase themselves from the canvas and from the arraylist once their x<0 (outside of the canvas) Please help me asap. Thanks
This is my code so far for the erase command:
for(Sprite sprite : rockSprites){
sprite.x -=10;
if (Rect.intersects(sprite.dst, die))
{
rockSprites.remove(this);
currentAmountOfSprites--;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
it creates more and more sprites and i passes the max int that i put (5).
回答1:
Should you not be using the line rockSprites.remove(sprite)
rather than using rockSprites.remove(this)
?
Since it's not listed, I am going to assume that your render phase actually uses the rockSprites
ArrayList(?) to determine what rock sprites should be put on the canvas?
来源:https://stackoverflow.com/questions/7989806/erase-an-object-from-canvas