Motm
Motm

Reputation: 19

"x cannot be resolved or is not a field". how can I fix this? PROCESSING

My variables for Enemy aren't "existing" in the GameScene function. I'm trying to make a game in processing but for some reason when I try to access a class, I get the error saying that "x cannot be resolved or is not a field" I will post my code below with all classes.

Scenes s = Scenes.Title; //this array stores the three scenes
Enemy [] en = new Enemy[100]; //spawns the monsters
Player p;        
ArrayList<Bullet> bulletList = new ArrayList<Bullet>();

PImage background;
PImage player;
PImage enemy;

boolean isDead;

void setup() {
  size(500, 500);
  textSize(18);
  text("", 0, 0);
  background = loadImage("backgrounddetailed5.png");
  player = loadImage("survivor-idle_handgun_0_up.png");
  enemy = loadImage("Monster.png");



  for (int n = 0; n < en.length; n++) {
    en[n] = new Enemy();
  }

  p = new Player(width/2, height/2);
}

void draw() {
  background(background);
    if (s == Scenes.Title) {
      TitleScene();
    } else if (s == Scenes.Gameplay) {
      GameScene(10);
      if (p.all_enemies_are_dead == true)
      {
        Enemy [] wave2 = new Enemy[20];
        p.all_enemies_are_dead = false;
        p.total_enemy_count = 20;
        GameScene(20);
      }
    }

enum Scenes {
  Title, 
    Gameplay, //1
    Win, //2
    Lose,
} 

below is Enemy class:

class Enemy {
  float x;
  float y;
  float size;
  float speed = random(1, 1.5);


  Enemy() {
    size = 45;
    x = random(0, width);
    while (y < 0 && y < height ) {  //this is the spawns of the enemies, i tried doing it
      y = random(0, -height * 20);  // like above the screen in game.
      x = random(0, width);
    }
  }

  //this function is the Ai of the enemy basically follows the player
  void follow() {
    if (x > p.loc.x) 
    {
      x -= speed;
    }

    if (x < p.loc.x) 
    {
      x += speed;
    }

    if (y > p.loc.y) 
    {
      y -= speed;
    }

    if (y < p.loc.y) 
    {
      y += speed;
    }
  }
}

this is my scene class where it loads the scene where the game starts:

void TitleScene(){
  fill(255);
   text("HIT SPACE TO START",width/2 - 100,height/2);
   if (keyPressed && key == ' '){
     s = Scenes.Gameplay;
   }
}

void GameScene(int number_of_enemies){

  background(background);
   fill(255);
   text("Wave 1",10,50);
   Enemy [] en = new Enemy[number_of_enemies];
   for (int i =0; i < number_of_enemies; i++) {

    image(enemy, en.x, en.y, en.size, en.size);
    en.follow();
    if (dist(p.loc.x + p.sz/2.5, p.loc.y + p.sz/2.5, en.x, en.y) < (p.sz + en.size)/2.5) {
        p.loc.x = 250;
        p.loc.y = 450; //restarts game
        isDead = true;
      }
    for (Bullet b : bulletList) {
      if (dist(en.x + en.size/2, en.y + en.size/2, b.p.x, b.p.y) < (en.size + b.size)/2) {
        en.size = 0; //despawns monsters
        p.total_enemy_count--;               
        if ( p.total_enemy_count <= 0 ) {    
            p.all_enemies_are_dead = true;   
        }
      }
    }
    if (dist(en.x + en.size/2, en.y + en.size/2, en.x, en.y) < (en.size + en.size)/2) {
      en.speed *= 1; //makes monsters collide with each other
    }
  }
  for (int n = 0; n < bulletList.size(); n++) {
    Bullet b = bulletList.get(n);
    b.run();
  }
  p.run();

}

void WinScene(){
  background(background); 
  fill(255);
   text("Hit R to play again",width/2 - 100,height/2);
   if (keyPressed && key == 'r')
     s = Scenes.Title;
}
void LostScene(){
 background(background); 
  fill(255);
  text("You are dead, hit R to play again",width/2 - 100,height/2);

}

The error I'm getting is in the GameScene Function, where it says "en.x, en.y and en.size" ... for some reason I do not know why every variable after en." " does not exists.

Upvotes: 1

Views: 1304

Answers (1)

CAMD_3441
CAMD_3441

Reputation: 3154

en is a type array Enemy[].

So every time you reference en you're supposed to reference the index in the array (such as en[i]) where you're trying to extract the variables, such as en.x, en.y, and en.size. Those should be en[i].x, en[i].y, and en[i].size.

Upvotes: 1

Related Questions