Why is old Text not replaced but overwritten

edited March 2015 in Questions about Code

Hi,

tried some variations of noLoop() and reddraw() but still one text is overwritten by new one. I would like to to have a clean write of the new text. What is wrong with that code

`void setup() {

size(640, 360, P3D);

noSmooth();

noLoop();

}

void draw() {

background(102);

drawType(width * 0.75);

}

void drawType(float x) {

text("One Text", x, 210);

delay(10000);

redraw();

text("Second Word", x, 210);

}`

?

Thanks for any hint

Tagged:

Answers

  • _vk_vk
    edited March 2015 Answer ✓
        /**
         * You should search the forum for:
         * timing and/or millis 
         * delay is not good for timing things
         * it halts the execution usually avoiding the expected render
         * to ocour. 
         **/
    
        /**
         * From Processing javaDoc:
         *
         * processing.org/reference/javadoc/core/
         *
         * The delay() function causes the program to halt for a specified time. 
         * Delay times are specified in thousandths of a second.
         * For example, running delay(3000) will stop the program for three seconds
         * and delay(500) will stop the program for a half-second. 
         * The screen only updates when the end of draw() is reached, 
         * so delay() cannot be used to slow down drawing. 
         * For instance, you cannot use delay() to control the timing of an animation. 
         * The delay() function should only be used for pausing scripts
         * (i.e. a script that needs to pause a few seconds before attempting a download,
         * or a sketch that needs to wait a few milliseconds before reading 
         * from the serial port).
         *
         **/
    
    
        /**********/
    
        /** 
         * Instead, use millis() or frameRate() to control timing, 
         * you might want to look at threads also...
         *
         * below a very simple way of doing it, there are many...
         * comments on code
         **/
    
    
    
        /**************************************************/
    
    
    
        //last Time we did something
        int lastT;
    
        // delay time 1000 = 1 sec
        int interval = 3000;
    
        // array with desired string
        String[] strings = {
          "One Text", "Second Word", "Even one more..."
        };
    
        // index of string to display
        int index = 0;
    
        // to keep things nicer : )
        PFont f;
    
    
        void setup() {
          size(640, 360);
    
          f = createFont("verdana", 18);
          textFont(f);
    
          //start the clock
          lastT  =millis();
        }
    
    
        void draw() {
    
          // clear screen every frame  
          background(102);
    
          // check how much time has passed
          if ( millis() - lastT > interval) {
    
            // if more than interval, move to next string
            // using modulo '%' to keep inside array boundaries
            index = ( index + 1 ) % (strings.length);
    
            // and reset de clock
            lastT = millis();
          } 
    
          // always draw desired string, now passed as a parameter
          drawType(width * 0.75, strings[index]);
        }
    
        void drawType(float x, String text) {
    
          // a really simple function : )
          text(text, x, 210);
        }
    

    Also there are great articles on the subject:

    I display images in sequence but I see only the last one. Why?

    How do I display a message for a few seconds?

  • thanks a lot, did work nicely ! However will have to read the links you added a view times again to understand it

Sign In or Register to comment.