Hey, I get random(!) errors while removing my bullet from ArrayList once it hits the player.
I think its because of the for loop but dont know how to do it different…
This is how I create the bullet:
ArrayList <Projectile> enemyBullets = new ArrayList<Projectile>(); // GegnerTyp2 Geschosse
projectile = new Projectile(enemyContainer.get(enemys).x, enemyContainer.get(enemys).y, sprites.projectile ,2);
enemyBullets.add(projectile);
This is the collision:
// Go through each enemy bullets
for(int x = 0; x < enemyBullets.size(); x++)
{
// Go through each enemy
for(int z = 0; z < enemyContainer.size(); z++)
{
// Does the bullet hit the player?
if(enemyBullets.get(x).x > player.x && enemyBullets.get(x).x < player.x + 64 + value
&& enemyBullets.get(x).y > player.y && enemyBullets.get(x).y < player.y + 37 )
{
// If the player has shield left it will damage this first.
if(player.shield>0)
player.shield = player.shield - 2f;
else
{
// The damage the player get
damageValue = (enemyContainer.get(z).laser - player.armor);
if(damageValue>0)
player.hp = player.hp - Math.abs(damageValue);
}
enemyBullets.get(x).sprite = sprites.emptyProjectile; // I remove the bullet graphic here.
enemyBullets.remove(x); //!!! Here I remove the bullet but I get random error.
}
}
}
So any ideas how I can solve it to remove the bullet without this error warning: