Josh Williams
Josh Williams

Reputation: 87

Why does my visualisation disappear after clicking a button using ControlP5 in Processing?

I am working on a application using Processing. I'd like to create two buttons using Control P5. After I press on of the keys, I want to show some content. However, it immediately disappears and I am not sure why.

setup()

import controlP5.*;

ControlP5 cp5;
controlP5.Button b;

void setup() {
    size(1080,720);
    background(210,210,210);
    cp5 = new ControlP5(this);

    b = cp5.addButton("A")
        .setPosition(100,140)
        .setSize(200,19);


    b = cp5.addButton("B")
       .setPosition(100,180)
       .setSize(200,19);
}

draw()

void draw() {
    background(0);
}

button functions()

public void A() {
    println("This is a");
    text("word", 100, 100);
}

public void B() {
    println("This is b");    
}

I'd like to know, how I would accomplish a situation - where I can click on button A, a visualisation shows up. And when I click on button B, a new visualisation shows up and hides the one from button A.

Because now, when I click on button A, 'word' shows for a second and disappears right away.

Upvotes: 1

Views: 292

Answers (1)

Ayush Seth
Ayush Seth

Reputation: 1209

This happens because A() is only called once whenever a button is pressed, unlike draw() which is called repeatedly and so you see only like a single frame of the text(). There is a way to fix this, basically you want to draw everything in draw() just choose what to draw and when to draw. You could do this using a boolean flag:

boolean drawText = false;

void draw() {
  background(0);
  if (drawText)
    text("word", 100, 100);
}

public void A() {
  drawText = true;
  println("This is a"); 
}

public void B() {
  drawText = false;
  println("This is b");
}

Upvotes: 4

Related Questions