Processing 3.3 中的矩阵文本雨效果
Matrix Text rain effect in Processing 3.3
我正致力于在 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 错误的奇怪部分和我不理解的部分是它被抛出在一个不接受任何可能返回空的参数的方法上,并且该方法本身是无效的,所以它不应该return什么都行,对吧?为什么这个 returning 为 Null?我做错了什么 return 这样的?
首先你想出一个介于 5 和 30 之间的随机数:
int totalSymbols = round(random(5, 30));
然后您创建一个数组,其中包含您的 Symbol
class:
的 500
个实例
Symbol[] symbols = new Symbol[500];
请注意,此时此数组包含 500 个 null
个值。
然后你最多添加 30 个 Symbol
实例到你的数组:
for (int i = 0; i <= totalSymbols; i++) {
symbols[i] = new Symbol(x, y, speed);
请注意,此时此数组现在至少包含 470 null
个值。
然后你遍历所有 500 个索引:
for(Symbol s : symbols) {
s.setToRandomSymbol();
但请记住,这些索引中至少有 470 个是 null
,这就是为什么您得到 NullPointerException
.
一些基本的调试会告诉你所有这些。我会在你收到错误之前添加一个基本的 println()
语句:
for(Symbol s : symbols) {
println("s: " + s);
s.setToRandomSymbol();
这会告诉您您正在迭代 null
个值。
无论如何,要解决您的问题,您需要停止遍历整个数组,或者您需要停止为您从未使用过的索引腾出空间。
以后,请 尝试在发帖前将您的问题缩小到 MCVE。请注意,这个小得多的示例程序显示了您的错误:
String[] array = new String[10];
array[0] = "test";
for(String s : array){
println(s.length());
}
我正致力于在 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 错误的奇怪部分和我不理解的部分是它被抛出在一个不接受任何可能返回空的参数的方法上,并且该方法本身是无效的,所以它不应该return什么都行,对吧?为什么这个 returning 为 Null?我做错了什么 return 这样的?
首先你想出一个介于 5 和 30 之间的随机数:
int totalSymbols = round(random(5, 30));
然后您创建一个数组,其中包含您的 Symbol
class:
500
个实例
Symbol[] symbols = new Symbol[500];
请注意,此时此数组包含 500 个 null
个值。
然后你最多添加 30 个 Symbol
实例到你的数组:
for (int i = 0; i <= totalSymbols; i++) {
symbols[i] = new Symbol(x, y, speed);
请注意,此时此数组现在至少包含 470 null
个值。
然后你遍历所有 500 个索引:
for(Symbol s : symbols) {
s.setToRandomSymbol();
但请记住,这些索引中至少有 470 个是 null
,这就是为什么您得到 NullPointerException
.
一些基本的调试会告诉你所有这些。我会在你收到错误之前添加一个基本的 println()
语句:
for(Symbol s : symbols) {
println("s: " + s);
s.setToRandomSymbol();
这会告诉您您正在迭代 null
个值。
无论如何,要解决您的问题,您需要停止遍历整个数组,或者您需要停止为您从未使用过的索引腾出空间。
以后,请 尝试在发帖前将您的问题缩小到 MCVE。请注意,这个小得多的示例程序显示了您的错误:
String[] array = new String[10];
array[0] = "test";
for(String s : array){
println(s.length());
}