Simple random string question

Hi, I’m trying to make the characters appear on the canvas one the character is pressed but they all seem to appear on the same location and overlap each other.
int x=int(random(25,450));
int y=int(random(25,450));
String word="";
void setup() {
size(500, 500);
}
void draw(){
textSize(25);
fill(0);
for(int i=0; i<word.length();i++)
text(word.charAt(i),x,y);
}
void keyPressed(){
word+=key;
}

Random

you need to have this in draw; the lines are only ran one and not throughout.

Better is this:

    x=int(random(25,450));
    y=int(random(25,450));

How draw() works

another sad truth: draw() updates the screen only once at its end and not throughout.

So everything that happens in the for-loop is added up on the screen and then displayed all at once on the screen at the end of draw(). Therefore you cannot see any animation using a for-loop. Bad.

To avoid this: Destroy the for-loop, say int i=0; before setup() and use the fact that draw() in itself loops automatically: say i++; in draw().

e.g. to slow this down by 10:

if(frameCount % 10 == 0) 
   i++;

Remark

…and implement i<word.length() …to avoid an error of course…

Chrisir

thank you for replying again:), I keep getting a string index error so I added the i<wordlength() aspect but i keep getting the same error. I also never new about that aspect of the draw function!

int x;
int y;
String word="";
int i=0;
void setup() {
  size(500, 500);
}
void draw() {
  x=int(random(25, 450));
  y=int(random(25, 450));
  textSize(25);
  fill(0);
  text(word.charAt(i), x, y);
  if(frameCount % 10 == 0 && i<word.length()) 
   i++;
  
}
void keyPressed() {
  word+=key;
}

Yeah, because word is empty???

APOLOGIES

obviously, this is better:


int x;
int y;
String wordMy="";
int i=0;

void setup() {
  size(500, 500);
}

void draw() {
  x=int(random(25, 450));
  y=int(random(25, 450));
  textSize(25);
  fill(0);
  if (wordMy.length() > 0)  //!!!
    text(wordMy.charAt(i), 
      x, y);
  if ((frameCount % 10 == 0) && (i<wordMy.length()-1))  //!!!!
    i++;
}

void keyPressed() {
  wordMy+=key;
}

Also, we could say i=0; when i>=wordMy.length()-1
or say i=0; in keyPressed

thank you for the modification, although they all seem to move randomly around the canvas, and only seem to appear once ,I’m trying to get all the characters appear on random locations on the screen and still(rigid) on the screen. I hope I’m conveying this correctly.

1 Like

Hm…

OK, draw() starts 60 times per second…

so first, the last letter is drawn again and again.

You could stop the whole process once i >= word.length() (using if(i<…) around the lines with random and those with text())

Then only the last letter would be added.

I’m sorry but I don’t think I understand? for instance if(i>wordlength() ){?)

changed this



int x;
int y;

String wordMy="";

int i=0;

void setup() {
  size(500, 500);
}

void draw() {


  if ( (i<wordMy.length())) {
    //!!!!


    x=int(random(25, 450));
    y=int(random(25, 450));
    textSize(25);
    fill(0);

    if (wordMy.length() > 0) { //!!!
      text(wordMy.charAt(i), 
        x, y);
    }


    i++;
  }
}

void keyPressed() {
  wordMy+=key;
}