“ x无法解析或不是字段”。我怎样才能解决这个问题?处理

时间:2019-03-17 21:16:49

标签: java processing

我的敌人变量在GameScene函数中不存在。我正在尝试制作一个游戏,但是由于某种原因,当我尝试访问一个类时,出现错误消息:“ x无法解析或不是字段”,我将在所有类的下面发布我的代码。 / p>

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

}

我得到的错误是在GameScene函数中,它说“ en.x,en.y和en.size” ...出于某种原因,我不知道为什么en之后的每个变量都如此。不存在。

1 个答案:

答案 0 :(得分:1)

en是类型数组Enemy[]

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