The sprite is spawned every second and when it's touched, it should be removed.
This is what I did:
//Render the sprites and making them move:
public void draw(SpriteBatch batch) {
for(Sprite drawEnemy:enemies) {
drawEnemy.draw(batch);
drawEnemy.translateY(deltaTime * movement);
touchInput(drawEnemy.getX(),drawEnemy.getY(),
drawEnemy.getWidth(),drawEnemy.getHeight(),drawEnemy);
}
}
//Detecting the touch input:
public void touchInput(float x,float y,float w,float h,Sprite sprite){
float touchX=Gdx.input.getX();
float touchY=Gdx.input.getY();
if(Gdx.input.justTouched()){
if(touchX > x && touchX < x+w ){
enemyIterator.remove();
Pools.free(sprite);
}
}
}
The touch input is detected, but I'm not sure how to remove them.
The result is an error when I touch them.
You cannot reuse an iterator, so your
enemyIterator
is invalid and will cause exceptions.To avoid needing to do this, change your
touchInput
method to simply test whether the object should be removed, not to remove it. Also note that you need to convert screen coordinates to world coordinates, so you must use the camera as well.You can only use the iterator at the place where you're iterating. So you must acquire a local reference at the loop like this:
However, the above is kind of muddy because you're mixing update and drawing code up together, and drawing before updating, and checking for touches over and over without need. I would redo it all like this:
Here you would call
update
beforedraw
from the owning class.