如何使数组中的每个单词在处理中彼此分开移动?
我知道这是一个逻辑错误,但我有这个程序,我想要显示50个随机单词,这50个单词应该展开,并且会随机移动,但是相反,我每帧一次收到50个随机单词他们都互相重叠,然后去随机的地方..我在我的代码中做错了什么?如何使数组中的每个单词在处理中彼此分开移动?
这里是我是如何做的:
String [] allWords;
int index = 0 ;
float x;
float y;
void setup() {
size (500,500);
background (255); //background : white
String [] lines = loadStrings ("alice_just_text.txt"); //imports the
external file
String text = join(lines, " "); //make into one long string
allWords = splitTokens (text, ",.?!:-;:()03 "); //splits it by word
x = 100; //where they start
y = 150;
}
void draw() {
background (255);
for (int i = 0; i < 50; i++) { //produces 50 words
x = x + random (-3,3); //makes the words move or shake
y = y + random (-3,3); //makes the words move or shake
int index = int(random(allWords.length)); //random selector of words
textSize (random(10,80)); //random font sizes
fill (0); //font color: black
textAlign (CENTER,CENTER);
text (allWords[index], x, y, width/2, height/2);
println(allWords[index]);
index++ ;
}
}
你有一对夫妇的问题。
首先,您只有一个x
和y
变量。您需要跟踪x
和y
变量,而不是每个词。你可以使用数组,或者更好的是你可以封装一个位置和一个单词。 (无耻的自我推销:我写了那个教程,但我强烈推荐阅读它,因为它包含几乎所有你想做的事情的例子。)
其次,你需要明确你在做什么for
在draw()
函数中循环。特别的是这行的作用:
int index = int(random(allWords.length)); //random selector of words
这是选择一个随机指数,但你这样做的draw()
函数内部for
循环中,所以这是发生50次,每秒60次。这可能不是你想要做的。
取而代之,您可能只是想要在setup()
函数中生成一次的随机单词。您可以通过创建您创建的类的实例并将它们存储在数组或ArrayList
中来实现。
是的,你是如此的乐于助人!并感谢您的所有建议!我在哪里也可以找到你的教程? –
@Noobprocessor它是我的文章中的[创建类](http://happycoding.io/tutorials/processing/creating-classes)链接。 –
哦好吧再次感谢youuu –
你的意思是:x = x + random(-3,3); ?你如何控制可以*填充的数字? –
这就是他们让这个词移动或摇动@VasylLyashkevych –
是的,我认为你不能编译你的代码,并且你使用-3作为边界之一。你有没有考虑过相同的算法?你可以使用:x = x + random.nextInt(3); –