我正在尝试编写一种boggle程序。我成功地在屏幕上生成了一些随机文本。现在我想将网格的每个部分链接到一个字母。坐标是关闭atm,但问题是当我点击区域X <= 50; Y&lt; = 50我得到一个空值,而我的数组的元素被不断地绘制,所以它不能是空的吗?
boolean clicked = false;
boolean Gen = true;
void setup() {
size(1000,1000);
textSize(64);
textAlign(CENTER);
}
void draw() {
String [] Storage = new String[17];
int x1 = 5;
int x2 = 5;
int x3 = 5;
int x4 = 5;
if(Gen) {
background(#AFA3A3);
// Loop for Storing
for(int i = 0; i < 17; i = i+1) {
String a;
a = genereren();
Storage[i]=a;
}
//Loop for Reading
//First Row
for(int j = 0; j < 4; j = j+1) {
text(Storage[j], 80+(x1*10), 125);
x1 = x1 +12;
}
//Second Row
for(int k = 4; k < 8; k = k+1) {
text(Storage[k], 80+(x2*10), 250);
x2 = x2 +12;
}
//Thrid Row
for(int c = 8; c < 12; c = c+1) {
text(Storage[c], 80+(x3*10), 375);
x3 = x3 +12;
}
//Fourth Row
for(int o = 12; o < 16; o = o+1) {
text(Storage[o], 80+(x4*10), 500);
x4 = x4 +12;
}
}
Gen = false;
for(int i=50; i<500;i=i+125){
noFill();
strokeWeight(5);
rect(i,50,125,125);
rect(i,175,125,125);
rect(i,300,125,125);
rect(i,425,125,125);
}
if(clicked==true && mouseX <= 50 && mouseY <= 50) {
text(Storage[1], 500,500);
}
Reset();
}
//draw
public String genereren() {
String alfabet = "abcdefghijklmnopqrstuvwxyz";
float r = random(24);
if(r < 1) {
r = r+1;
}
int d = int(r);
String EersteLetter;
EersteLetter = alfabet.substring(d-1,d);
return EersteLetter;
}
public void Store() {
}
public void mouseClicked() {
clicked = true;
}
public void Reset() {
clicked = false;
}
答案 0 :(得分:3)
发布代码时,请使用理智的格式和标准命名约定(存储应该是存储等)。
无论如何,你在draw函数中初始化你的Storage变量,这意味着你每秒钟都会创建一个新的空数组。但是,如果Gen为true,则只使用值填充该数组,这只会在第一次调用draw()时发生。
这意味着在以后调用draw()函数时,Storage为空,这意味着它包含所有空值。然后当你点击左上角时,它会将一个空值传递给text()函数,这会导致NPE。
大多数Processing草图都调用background()作为draw()函数的第一行。添加它,你会发现在后续调用draw()函数时你实际上并没有填充数组。您可能希望在setup()函数中填充它,然后在每次调用draw()时绘制它。