Home > database >  I'm trying to iterate through a list of Objects, using their values for collision detection. Ja
I'm trying to iterate through a list of Objects, using their values for collision detection. Ja

Time:01-20

Im making a 2D space shooter game with an array list of enemies each with their own values and PVectors. I need to iterate through the array list and compare the values of each enemy to the value of the player projectile.

My collision code where im getting the error on line 4.

void collision(){
  
  for(int c = 0; c < enemies.size(); c  ) {
    Enemy =  enemies.get(c);
    if(projectilePos.x < enemyPos.x   enemyWidth &&
    projectilePos.x   projectileWidth > enemyPos.x &&
    projectilePos.y < enemyPos.y   enemyHeight &&
    projectileHeight   projectilePos.y > enemyPos.y){
      //colliding
      enemies.remove(c);
    }
  }

My PVectors and array lists.

//PVectors
PVector playerPos = new PVector(500, 500);
PVector projectilePos;
PVector enemyPos;

// Arrays
ArrayList<playerProjectile> PProjectiles = new ArrayList<playerProjectile>();
ArrayList<Enemy> enemies = new ArrayList<Enemy>();

//collision values
float enemyWidth = 20;
float enemyHeight = 20;
float projectileWidth = 20;
float projectileHeight = 20;

My enemy Class

class Enemy
{
  float enemyX = 0;
  float enemyY = 0;
  float enemyMoveX;
  float enemyMoveY;
   
  Enemy(float startX, float startY){
    startX = random(1000);
    startY = 0;
    enemyPos = new PVector(startX, startY);
  }
  
  void createEnemy(){
    if (millis() - time >=2000){
      Enemy foe = new Enemy (enemyPos.x, enemyPos.y);
      enemies.add(foe);
      time = millis();
    }
  }
    
  void updateEnemy(){
   float b;
   float c;
   float speed = 1;
   float movement;
   float a = 1000 - enemyPos.y;
   b = 500 - enemyPos.x;
   c = sqrt((a * a)   (b * b));
   movement = speed / c;
   enemyMoveX = enemyX - (movement * b);
   enemyMoveY = enemyY -(movement *a);
   
   pushMatrix();
    translate(enemyPos.x, enemyPos.y);
    fill(255, 0, 64);
    ellipse(0, 0, 20, 20);
   popMatrix();
   
   enemyPos.x = enemyPos.x - enemyMoveX;
   enemyPos.y = enemyPos.y - enemyMoveY;
  }
  
}

My projectile class.

class playerProjectile {
  float projectileSpeedX = deltaX;
  float projectileSpeedY = deltaY;

  
 
  
  playerProjectile(float startX, float startY){
    projectilePos = new PVector(startX, startY);
  }
  
  void updateprojectile()
  {
    pushMatrix();
    translate(projectilePos.x, projectilePos.y);
    fill(234, 0, 255);
    ellipse(0, 0, 20, 20);
  popMatrix();
    projectilePos.x = projectilePos.x - projectileSpeedX;
    projectilePos.y = projectilePos.y - projectileSpeedY;
    
    
   
  }
  int maxlength = 150;
  void removeBullets()
  {
    while(PProjectiles.size() >maxlength){
      PProjectiles.remove(0);
    }
  }
}

I've tried looking at examples of similar pieces of code but nothing everything i have tried has come out with an error.

CodePudding user response:

You need to define a object name to receive a value from enemies:

Enemy =  enemies.get(c);
Enemy enemy =  enemies.get(c);
  • Related