I have this program that I need 50 random words from an external file to appear and each of them randomly moves to a random place... I was already able to make each word move separately from each other to a random place but the problem is that only the first word in the external file appears 50 times and that is the only word that appears... not 50 random words! just 50 words that are all the same... so I tried to put int index = int(random(allWords.length));
under draw
and inside for
but this may cause that it will happen 50 times, 60 times per second and that's not what I want to happen... someone suggested that Instead, I could probably just want to generate the random words once in the setup()
function and I could do this by creating instances of the class
I created and store them in an array or an ArrayList. The thing is I'm still not that familiar with that so does anybody has tips on how I can do that or maybe a link where I can have a guide on how to do that?
here is my code if anyone wants to see what my problem is...
String [] allWords;
int x = 120;
int y = 130;
int index = 0 ;
word [] words;
void setup () {
size (500, 500);
background (255); //background : white
String [] lines = loadStrings ("alice_just_text.txt");
String text = join(lines, " "); //make into one long string
allWords = splitTokens (text, ",.?!:-;:()03 "); //splits it by word
words = new word [allWords.length];
for (int i = 0; i < 50; i++) {
words[i] = new word (x, y);
}
}
void draw() {
background (255);
for (int i = 0; i < 50; i++) { //produces 50 words
words[i].display();
words[i].move();
words[i].avgOverlap();
}
}
class word {
float x;
float y;
word(float x, float y) {
this.x = x;
this.y = y;
}
void move() {
x = x + random(-3, 3); //variables sets random positions
y = y + random(-3, 3); //variables sets random positions
}
void display() {
fill (0); //font color: black
textAlign (CENTER, CENTER);
text (allWords[index], x, y, width/2, height/2 );
}
void ran () {
textSize (random(10, 80)); //random font size
}
}
Aucun commentaire:
Enregistrer un commentaire