Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing from an arraylist android

Tags:

java

arraylist

I have an android game which involves a ship shooting enemies. I am trying to make it so that if the enemies are within a certain distance of the ammos, then the enemys remove themselves from the screen. I have attempted to do it and the code compiles, but i am unsure why the enemys arent being removed from the screen once been hit. Can anyone see anything wrong with the code below? Thankyou

for (TopEnemy i : newTopEnemy)
{
    for (int q = 0; q < ammo.length; q++)
    {
       float xsubs = i.enemyX - ammo[q].positionX;
       float ysubs = i.enemyY - ammo[q].positionY;
       float squared = (xsubs * xsubs) + (ysubs * ysubs);
       float distance = (float)Math.sqrt(squared);
       if (distance < 10.0)
       {
          newTopEnemy.remove(q);
       }
    }
 }  
like image 589
user3426043 Avatar asked Aug 23 '26 17:08

user3426043


1 Answers

Shouldn't it be newTopEnemy.remove(i); ? q looks like an index on ammo.

like image 191
Vlad Avatar answered Aug 25 '26 06:08

Vlad