Text() and Delay()

I want text to appear on the screen, but letter by letter and slowly.
When I execute this code nothing is displayed to the user and I’m unsure as to why.

String textLine = “This is a test to see if I can do this”;
String output=“”;
boolean continueRunning = true;

void setup() {
size(1240, 980);
}

void draw() {
while(continueRunning ==true){
for(int i = 0;i<textLine.length();i++){
output+=textLine.charAt(i);
text(output,100,400);
delay(1000);
}
continueRunning=false;
}
}

-My thought process was to see if I could manage to loop through textLine, pull the letter from that index and add it onto the end of the output, then delay and repeat with the next index.
-I figured by doing this output should eventually be equal to textLine and be displayed with Text() through each iteration of the loop.
-The boolean continueRunning is just so the loop happens once.

  • pls format your code with

</>

  • how you start coding?
    you complain you see NOTHING
    ok, i test a reduction of your code and also
    see a big empty window.
//String textLine = "This is a test to see if I can do this";
String output="test";
//boolean continueRunning = true;

void setup() {
  size(1240, 980);
}

void draw() {
//  while (continueRunning ==true) {
//    for (int i = 0; i<textLine.length(); i++) {

//      output+=textLine.charAt(i);

      text(output, 100, 400);
//      delay(1000);
//    }
//    continueRunning=false;
//  }
}

possibly you work on this until you see some text

later we do the next step.

Don’t use delay(). It is not the function you want. It does not do what you think it does. It is not useful in this situation. Do not use delay(). Do not use delay(). Do not use delay(). Do not use delay(). Do not use delay().

Do not use delay().

DO NOT-

You get the point.

Use millis()!

1 Like
int time;
int n;

void setup() {
  size(400, 400);
  time = millis() + 1500;
  n = 1;
}

void draw() {
  background(0);
  for ( int i = 0; i < n; i++) {
    ellipse(15 + 30 * i, 15, 20, 20);
  }
  if ( millis() > time ) {
    n++;
    time = millis() + 1500;
  }
  if ( n > 10 ) {
    n = 1;
  }
}

Imagine what you could do with the n variable, your String, and the substring() function…

1 Like

I do not have the same issue you have. If I run the code with the reductions you had the word “test” appears.

very good, now start with a timer



String textLine = "This is a test to see if I can do this ";
String output = "";
int i = 0;
// use basic millis timer
long startT, actionT=500, nowT;

//_______________________________________________
void my_timer() {
  nowT = millis();
  if ( nowT > startT + actionT ) {
    startT = nowT;
    do_something();
  }
}

//_______________________________________________
void do_something() {
  output += textLine.charAt(i);
  println("i: "+i+" char: "+output);
  i++; 
  if ( i >= textLine.length() ) { 
    i = 0; 
    output = "";
  }
}

//_______________________________________________
void setup() {
  //  size(1240, 980);
  size(300, 100);
  startT = millis();
}

//_______________________________________________
void draw() {
  background(200, 200, 0);
  //stroke(0, 0, 200);
  fill(0, 0, 200);
  text(output, 10, 50);
  my_timer();
}



pls. note the print line,

println("i: "+i+" char: "+output);

this is the basic way to do diagnostic, so you know where you are and what is happening

1 Like

Thank you so much to the both of you for your help. Do you think you could explain why delay() wasn’t what I wanted?
https://processing.org/reference/delay_.html

From it’s description it sounds as if it just stops the program for a little while.

Once again though, thank you so much. millis() is what I needed.

Imagine there is a robot painter. He puts some paint on a canvas and shows it to you. It is a nice picture. But the robot is a very fast painter. It immediately stops showing you the picture and paints another one. And it keeps doing this.

If the picture he painted was the same each time, it would look like it was just a painting. But if the robotic painter painted a slightly different picture each time, it would look like an ANIMATED painting.

The robotic painter is draw(). It draws a picture, and then when it is done, it shows you the resulting image for one frame.

When you call delay, you are not saying “Stop drawing pictures for a while and let me keep looking at the last one.” NO! You are telling the robot painter to take a break in the middle of making a painting. You don’t get to see any new frames until his break is over!

So really, what delay says is “Stop drawing paintings for some amount of time AND ALSO STOP SHOWING ME THEM.” So not only does the program stop drawing, it ALSO stops updating what you are seeing! Yikes! This is NOT what you want. Delay is NOT doing what you thought it would do!

Using millis() is like having the robotic painter look at a ticking clock, and using the current time to know what to paint. It’s a much better way of drawing things based on elapsed time.

3 Likes

that actually would be the shortest version:


String textLine = "This is a test to see if I can do this ";
int i = 0;
long startT, actionT=500, nowT;   // use basic millis timer
//_______________________________________________
void my_timer() {
  nowT = millis();
  if ( nowT > startT + actionT ) {
    startT = nowT;
    i++; 
    if ( i >= textLine.length() )  i = 0;
  }
}
//_______________________________________________
void setup() {
  size(300, 100);
  fill(0);
}
//_______________________________________________
void draw() {
  background(255);
  text(textLine.substring(0, i), 10, 50);
  my_timer();
}