如何从 ArrayList 中删除 Sprite/Object?
How to remove Sprite/Object from ArrayList?
我为需要删除的精灵创建了一个 ArrayList,当精灵被触摸时,它们被添加到 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
}
}
我不太确定你在问什么,但如果你想从 ArrayList 中删除精灵,你可以使用 ArrayList.remove 方法。您需要知道要删除的 Sprite 的索引,就像访问任何数组时需要该信息一样。
Iterator<Sprite> it = removeEne.iterator();
while (it.hasNext()) {
it.remove();
}
我为需要删除的精灵创建了一个 ArrayList,当精灵被触摸时,它们被添加到 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
}
}
我不太确定你在问什么,但如果你想从 ArrayList 中删除精灵,你可以使用 ArrayList.remove 方法。您需要知道要删除的 Sprite 的索引,就像访问任何数组时需要该信息一样。
Iterator<Sprite> it = removeEne.iterator();
while (it.hasNext()) {
it.remove();
}