Class "Nebula" 不存在

Class "Nebula" does not exist

我在使用 Processing 3.3 时遇到问题。我刚刚开始使用一种星云模拟器,旨在模拟恒星从星云到红巨星的诞生和生命周期。到目前为止,我已经创建了两个 classes:Gas,用于每个单独的气体粒子,以及 Nebula,指的是粒子的集合。我在编辑器中输入了以下代码,每次都得到相同的结果:'Class "Nebula" does not exist.' 我的代码经过大幅简化,如下所示:

煤气:

class Gas {
  /* variables, constructor, etc. */

  void applyGravity(Nebula n) {
    /* code to apply force of gravity of
       the nebula to the particle */
  }
}

星云:

class Nebula {
  ArrayList<Gas> particles; // array of particles

  /* variables, constructor, etc. */
}

奇怪的是,我在 Nebula class 中没有得到 'Class "Gas" does not exist' 的错误,但在 Gas class 中却得到了 'Class "Nebula" does not exist' 的错误。

我试过退出并重新打开文件,以及重新安装 Processing。任何帮助将不胜感激。

基本上,Processing 编辑器可以处理两种类型的代码。第一种是函数调用的基本列表,像这样:

size(500, 200);
background(0);
fill(255, 0, 0);
ellipse(width/2, height/2, width, height);

使用这种类型的代码,Processing 一次只运行一个命令。

第二种代码是"real"带有函数定义的程序,像这样:

void setup(){
   size(500, 200);
}

void draw(){
    background(0);
    fill(255, 0, 0);
    ellipse(width/2, height/2, width, height);
}

对于这种类型的代码,Processing 在开始时调用一次 setup() 函数,然后每秒调用 draw() 函数 60 次。

但请注意,您不能拥有混合两种类型的代码:

size(500, 200);

void draw(){
    background(0);
    fill(255, 0, 0);
    ellipse(width/2, height/2, width, height);
}

这会给你一个编译器错误,因为 size() 函数不在函数内部!

您的代码是怎么回事,Processing 发现您没有定义任何草图级函数,因此它尝试将其视为第一类代码。但随后它看到 class 定义,这些定义仅在第二种代码中有效。这就是您收到错误的原因。

要解决您的问题,只需在您的代码中定义一个 setup() and/or draw() 函数,这样 Processing 就知道它是一个 "real" 程序。