How to remove Sprite/Object from ArrayList?

393 views Asked by At

I created an ArrayList just for the sprites that needs to be removed, when the sprites are touched they are added to the ArrayList.

 //drawing the enemy that spawns and making them move

        public void draw(SpriteBatch batch){
            for(Sprite drawEnemy:enemies) {
                drawEnemy.draw(batch);
                drawEnemy.translateY(deltaTime * movement);
            touchInput(drawEnemy.getX(),drawEnemy.getWidth(),drawEnemy);//2nd method
            }

        }


     public void touchInput(float x,float w,Sprite sprite){
            float touchX=Gdx.input.getX();

            if(Gdx.input.justTouched()){
                if(touchX > x && touchX < x+w ){
                   removeEne.add(sprite);// Adding the current Sprite to the array list when touched
                }                        //removeEne is my ArrayList
            }

        }
2

There are 2 answers

0
Uma Kanth On BEST ANSWER
Iterator<Sprite> it = removeEne.iterator();
while (it.hasNext()) {

    it.remove();

}
0
Andrew On

I'm not quite sure what you're asking, but if you want to remove sprites from your ArrayList, you can use the ArrayList.remove method. You would need to know the index of the Sprite you'd like to remove, just like you'd need that information when accessing any array.