首页 > 解决方案 > “x 无法解析或不是字段”。我怎样才能解决这个问题?加工

问题描述

我的 Enemy 变量在 GameScene 函数中并不“存在”。我正在尝试制作一个正在处理的游戏,但由于某种原因,当我尝试访问一个类时,我收到一条错误消息,说“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);

}

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

标签: javaprocessing

解决方案


en是一个类型数组Enemy[]

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


推荐阅读