如何使数组中的每个单词在处理中彼此分开移动?

how to make each word in an array move separately from each other in processing?

我知道这是一个逻辑错误,但我有这个程序,我想要显示 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++ ;


 }

}

你有几个问题。

首先,您只有一个 xy 变量。您需要跟踪 每个单词 xy 变量。您可以为此使用数组,或者更好的是,您可以 create a class 封装一个位置和一个词。 (无耻的自我推销:我写了那个教程,但我强烈建议你阅读它,因为它包含的示例几乎可以完成你想做的所有事情。)

其次,您需要准确了解在 draw() 函数的 for 循环中您在做什么。这一行的具体作用是:

int index = int(random(allWords.length));  //random selector of words

这是在选择一个随机索引,但是您是在 draw() 函数内的 for 循环中进行的,所以这会发生 50 次,每秒 60 次。这可能不是你想要做的。

相反,您可能只想在 setup() 函数中生成随机单词 一次。您可以通过创建您创建的 class 的实例并将它们存储在数组或 ArrayList.

中来做到这一点