Java &引用;x无法解析或不是字段;。我怎样才能解决这个问题?处理

Java &引用;x无法解析或不是字段;。我怎样才能解决这个问题?处理,java,processing,Java,Processing,我的敌人变量在游戏场景函数中不“存在”。我试图在处理中制作一个游戏,但由于某些原因,当我尝试访问一个类时,我得到一个错误,说“x无法解析或不是一个字段”,我将在下面发布我的代码和所有类 Scenes s = Scenes.Title; //this array stores the three scenes Enemy [] en = new Enemy[100]; //spawns the monsters Player p; ArrayList<Bullet> b

我的敌人变量在游戏场景函数中不“存在”。我试图在处理中制作一个游戏,但由于某些原因,当我尝试访问一个类时,我得到一个错误,说“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,
} 
Scenes s s=Scenes.Title//此阵列存储三个场景
敌人[]en=新敌人[100]//产生怪物
玩家p;
ArrayList bulletList=新建ArrayList();
年龄背景;
皮梅杰球员;
皮迈格敌人;
布尔型isDead;
无效设置(){
大小(500500);
文本大小(18);
文本(“,0,0);
background=loadImage(“backgrounddetailed5.png”);
player=loadImage(“survivor-idle_handgun_0_up.png”);
敌人=加载图像(“Monster.png”);
对于(int n=0;n
下面是敌人的等级:

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;
    }
  }
}
类敌人{
浮动x;
浮动y;
浮子大小;
浮动速度=随机(1,1.5);
敌人(){
尺寸=45;
x=随机(0,宽度);
当(y<0&&yp.loc.x)
{
x-=速度;
}
如果(xp.loc.y)
{
y-=速度;
}
如果(y
这是我的场景类,它加载游戏开始的场景:

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

}
void TitleScene(){
填充(255);
文本(“点击空格开始”,宽度/2-100,高度/2);
如果(按键&&key=''){
s=场景。游戏性;
}
}
无效游戏场景(敌人的整数){
背景(背景);
填充(255);
文本(“第1波”,10,50);
敌人[]en=新敌人[敌人的数量];
for(int i=0;i<敌人的数量;i++){
图像(敌人,en.x,en.y,en.size,en.size);
en.follow();
如果(距离(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;//重新开始游戏
isDead=true;
}
对于(项目符号b:项目符号列表){
如果(距离(en.x+en.size/2,en.y+en.size/2,b.p.x,b.p.y)<(en.size+b.size)/2){
en.size=0;//消灭怪物
p、 总敌人计数--;

如果(p.total_敌军_count
en
是一个类型数组
敌军[]


因此,每次引用
en
时,您都应该引用数组中的索引(例如
en[i]
),在数组中您试图提取变量,例如
en.x
en.y
,和
en.size
。这些索引应该是
en[i].x
en[i].y
,和
en[i]。size
,是什么“处理?”这是你的学校作业吗?@Zephyr是一本灵活的软件速写本和一种学习如何编码的语言……请查看标签上的信息!