处理 2:来自数组的随机文本在点击时出现?
Processing 2: Random text from array to appear on click?
电影名言只是随机快速连续出现,而不是从文本文档中随机抽取一个电影名言的预期效果。
理想情况下,我希望通过单击鼠标来显示新报价。
尝试将数组和索引设为全局变量,但由于某种原因无法显示文本。
PImage wallpaper;
void setup() {
size(600, 600);
wallpaper = loadImage("Theatrescreen.png");
}
void draw() {
background(wallpaper);
String[] moviequotes = loadStrings("moviequotes.txt");
int index = int(random(moviequotes.length));
text(moviequotes[index], mouseX, mouseY);
}
void mousePressed() {
}
您对 Random 的实现不正确。尝试这样的事情:
void draw() {
background(wallpaper);
String[] moviequotes = loadStrings("moviequotes.txt");
Random randIndex = new Random();
int index = randIndex.nextInt(moviequotes.length); // generate a random index from 0 to movieqoutes.length
text(moviequotes[index], mouseX, mouseY);
}
您将在每个抽奖周期生成一个新的随机电影名言。这意味着每秒很多(超过 25 取决于帧速率)。
您可以这样设置帧率:
void setup() {
frameRate(1);
}
或者在你的绘制循环中添加一个计数器,这样你就可以偶尔生成一个新报价
draw()
中的代码在无限循环中执行。我认为那是你的问题。参见 Processing Reference - draw(). To take care of the problem, consider using noloop()
. See Processing Reference - noloop()。
电影名言只是随机快速连续出现,而不是从文本文档中随机抽取一个电影名言的预期效果。
理想情况下,我希望通过单击鼠标来显示新报价。
尝试将数组和索引设为全局变量,但由于某种原因无法显示文本。
PImage wallpaper;
void setup() {
size(600, 600);
wallpaper = loadImage("Theatrescreen.png");
}
void draw() {
background(wallpaper);
String[] moviequotes = loadStrings("moviequotes.txt");
int index = int(random(moviequotes.length));
text(moviequotes[index], mouseX, mouseY);
}
void mousePressed() {
}
您对 Random 的实现不正确。尝试这样的事情:
void draw() {
background(wallpaper);
String[] moviequotes = loadStrings("moviequotes.txt");
Random randIndex = new Random();
int index = randIndex.nextInt(moviequotes.length); // generate a random index from 0 to movieqoutes.length
text(moviequotes[index], mouseX, mouseY);
}
您将在每个抽奖周期生成一个新的随机电影名言。这意味着每秒很多(超过 25 取决于帧速率)。
您可以这样设置帧率:
void setup() {
frameRate(1);
}
或者在你的绘制循环中添加一个计数器,这样你就可以偶尔生成一个新报价
draw()
中的代码在无限循环中执行。我认为那是你的问题。参见 Processing Reference - draw(). To take care of the problem, consider using noloop()
. See Processing Reference - noloop()。