【发布时间】:2018-01-01 18:05:30
【问题描述】:
我正在尝试在 Processing 中制作一个简单的 3d 游戏,但我遇到了问题。 我尝试创建一个数组来跟踪我的环境对象并使其更易于修改。但是,当我尝试运行该程序时,它不起作用。
主要代码:
//arrays
BoxCl[] envArr;
void setup() {
size(640, 360, P3D);
envArr[0] = new BoxCl(1,1,-1,1); //it shows the error here
envArr[envArr.length] = new BoxCl(2,1,-1,1);
}
void draw() {
background(0);
camera(mouseX, height/2, (height/2) / tan(PI/6), width/2, height/2, 0, 0, 1, 0);
Update();
Draw();
}
void Update(){
}
void Draw(){
for(BoxCl i : envArr){
i.Draw();
}
}
BoxCl 类:
class BoxCl{
float x, y, z;
int s;
BoxCl(float x, float y, float z, int size){
this.x = x;
this.y = y;
this.z = z;
this.s = size;
}
void Draw(){
translate(scale*x, scale*y, scale*z);
stroke(255);
fill(255);
box(s * scale);
translate(scale*-x, scale*-y, scale*-z);
}
}
我已尝试查找 (here for example),但我认为我太缺乏经验,无法理解我应该做什么。
请帮忙。
edit:我知道变量/数组/对象应该在使用之前定义。但是我如何以仍然可以改变的方式定义 envArr 呢? (即当我必须创建或删除对象时增加或减少大小)
【问题讨论】:
-
TL;DR:您从未初始化数组
envArr -
您的编辑应该作为一个单独的问题发布,并带有一个新的minimal reproducible example。
标签: java arrays object nullpointerexception processing