erase an object from canvas

…衆ロ難τιáo~ 提交于 2020-01-07 04:58:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!