erase an object from canvas

2019-09-02 02:02发布

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条回答
可以哭但决不认输i
2楼-- · 2019-09-02 02:32

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?

查看更多
登录 后发表回答