【发布时间】:2017-07-11 04:18:38
【问题描述】:
我正在 Processing 3.3 中制作矩阵文本雨效果,作为学习处理库和 Java 的简单入门项目。到目前为止我的代码:
class Symbol {
int x, y;
int switchInterval = round(random(2, 50));
float speed;
char value;
Symbol(int x, int y, float speed) {
this.x = x;
this.y = y;
this.speed = speed;
}
//Sets to random symbol based on the Katakana Unicode block
void setToRandomSymbol() {
if(frameCount % switchInterval == 0) {
value = char((int) random(0x30A0, 0x3100));
}
}
//rains the characters down the screen and loops them to the top when they
// reach the bottom of the screen
void rain() {
if(y <= height) {
y += speed;
}else {
y = 0;
}
}
}
Symbol symbol;
class Stream {
int totalSymbols = round(random(5, 30));
Symbol[] symbols = new Symbol[500];
float speed = random(5, 20);
//generates the symbols and adds them to the array, each symbol one symbol
//height above the one previous
void generateSymbols() {
int y = 0;
int x = width / 2;
for (int i = 0; i <= totalSymbols; i++) {
symbols[i] = new Symbol(x, y, speed);
symbols[i].setToRandomSymbol();
y -= symbolSize;
}
}
void render() {
for(Symbol s : symbols) {
fill(0, 255, 70);
s.setToRandomSymbol();
text(s.value, s.x, s.y);
s.rain();
}
}
}
好的,那是很多代码,让我解释一下我的困境。我遇到的问题是,当我运行代码时,我在渲染函数的 for each 循环中的 s.setToRandomSymbol(); 方法调用处得到 NullpointerException。关于这个 NullPointerException 错误的奇怪部分和我不理解的部分是它被抛出一个不接受任何可能返回为空的参数的方法,并且该方法本身是无效的,所以它不应该什么都可以退,对吧?为什么这会返回 Null,我做错了什么让它以这种方式返回?
【问题讨论】:
-
什么是
width,你在哪里设置它? -
width是处理库中的内置变量,等于窗口的宽度。 -
请发帖minimal reproducible example。当我尝试运行您的代码时,我收到一个错误,因为从未定义过
symbolSize。即使我修复了那个错误,我也只是得到一个空白草图,因为你没有包含你的setup()或draw()函数。与其发布所有代码,不如尝试创建一个我们可以运行的小示例,然后我们将从那里开始。 -
谢谢,我一定会在以后的帖子中这样做。
标签: processing