"x cannot be resolved or is not a field" .我该如何解决这个问题?加工



我对敌人的变量不是游戏定局功能中的"存在"。我正在尝试制作处理游戏的游戏,但是由于某种原因,当我尝试访问课程时,我会发现" X无法解决或不是字段"的错误,我将在下面的所有类中发布我的代码。<<<<<<<<<<<<

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,
} 

下面是敌人级别:

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;
    }
  }
}

这是我的场景课,它加载了游戏开始的场景:

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);
}

我遇到的错误是在GamesScene函数中,它说" en.x,en.y and en.size" ...由于某些原因,我不知道为什么在EN之后的每个变量。"不存在。

en是一种类型Array Enemy[]

因此,每次您参考en时,您都应该在数组(例如en[i])中引用索引,其中您要尝试提取变量,例如en.xen.yen.size。这些应该是en[i].xen[i].yen[i].size

最新更新