我知道這是一個邏輯錯誤,但我有這個程序,我想要顯示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 = x + random(-3,3); ?你如何控制可以自由填充的數字? –
這就是他們讓這個詞移動或搖動@VasylLyashkevych –
是的,我認爲你不能編譯你的代碼,並且你使用-3作爲邊界之一。你有沒有考慮過相同的算法?你可以使用:x = x + random.nextInt(3); –