Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I'm trying to iterate through a list of Objects, using their values for collision detection. Java processing

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

//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.

>Solution :

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

Enemy =  enemies.get(c);
Enemy enemy =  enemies.get(c);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading