Change Text by Clicking

How would you change the text in a specific area by clicking.

I tried void mousePressed but am having difficulty with getting the old number to disappear.

Answers

  • edited April 2015 Answer ✓

    Hello,

    first you need a global variable:

    boolean textHasBeenClicked = false;

    in draw() :

    say

    void draw() {
        background(0);
        if (textHasBeenClicked) {
            // display text 2
            text("has been pressed" , 100,100); 
        }
        else  {
            // display text 1
            text("please press" , 100,100); 
        }
    } 
    

    and

    void mousePressed() {
        // toggle 
        textHasBeenClicked = ! textHasBeenClicked;
    }
    

    Thus the var shows the program what to display.

    • You have one part where you tell the var what state it has and
    • one part where you read out this var.

    Often beginners don't use a var like that but try to display the different text when the mouse is clicked. Won't work.

    Alternatively

    Alternatively you can display a text in draw() via a String var.

    String myText = "please press ";

    in mousePressed() you then would change the var myText.

    No If necessary in draw() then. ;-)

    To take this further, when you have three different texts you would instead of a boolean use an int var that can only have values 0,1,2...

    To organize a whole program with this approach google states.

    Chrisir ;-)

  • boolean textHasBeenClicked = false;

    of course

    ;-)

Sign In or Register to comment.