Make bouncy text stop at certain point?

edited October 2016 in Questions about Code

Hullo. I am a 8th grader whose doing processing for an assignment. I saw a post by tfguy44 (Animated text) and the script for BouncyText. How do you make it stop at a certain Point? I would like it to stop in the middle of the canvas. Here's the script so far: class bouncyWord { String theWord; float px, py, vx, vy; bouncyWord(String iWord, float ipx) { theWord = iWord; px=ipx; vx=0; py=height/1; vy=random(2, 7); } void setup(){ size(450,450); } void draw() { px+=vx; py+=vy; if (py<0) { py=0; vy=-vy; } if (py>height) { py=height; vy=-vy; } text(theWord, px, py); } }

bouncyWord aa, bb, cc, dd, ee, ff;

void setup() {
  size(220, 220);
  textAlign(CENTER);
  aa = new bouncyWord("nugget", width/4); 
  fill(255);
}

void draw() {
  background(0);
  aa.draw();
}
Tagged:

Answers

  • @Nugget -- Please edit your post, then highlight and format the code with CTRL-o. This will aid other forum members in reading and testing your code.

  • Have a look at the following changes:

    class bouncyWord { 
      String theWord; 
      float px, py, vx, vy; 
      boolean bounce;   //  ***  ADDED
      bouncyWord(String iWord, float ipx) { 
        theWord = iWord; 
        px=ipx; 
        vx=0; 
        py=height/1; 
        vy=random(2, 7);
        bounce=true;    //  ***  ADDED
      } 
      void setup() { 
        size(450, 450);
      } 
    
      void toggle() {     //  ***  ADDED
        bounce=!bounce;  //  ***  ADDED
      }                  //  ***  ADDED
    
      void draw() { 
    
    
        if (bounce==true) {    //  ***  ADDED
          px+=vx; 
          py+=vy; 
          if (py<0) { 
            py=0; 
            vy=-vy;
          } 
          if (py>height) { 
            py=height; 
            vy=-vy;
          }
        }    //  ***  ADDED
        text(theWord, px, py);
      }
    }
    
    
    bouncyWord aa, bb, cc, dd, ee, ff;
    
    void setup() {
      size(220, 220);
      textAlign(CENTER);
      aa = new bouncyWord("nugget", width/4);
      fill(255);
    }
    
    void draw() {
      background(0);
      aa.draw();
    }
    
    void mouseReleased() {     // ***   ADDED
      aa.toggle();
    }
    

    I hope this helps,

    Kf

  • (you need a blank line before and after the code for it to show correctly. you need blank lines between paragraphs generally)

Sign In or Register to comment.