processingcontrol-p5

Buttons with classes


I'm using Processing 3 and the Controlp5 library.

Let's say I had a a method that was apple.eat(). How would I put that in a button?

cp5.addButton("apple.eat") doesn't work.

How would I make the button trigger apple.eat()?


Solution

  • An alternative way is to add a CallbackListener to the button.

    Button eat = new Button(cp5, "eat apple");
    
    eat.addCallback(new CallbackListener() {        
        @Override
        public void controlEvent(CallbackEvent event) {
            if (event.getAction() == 100) {
                apple.eat();
            }
        }
    });