Zac Stewart
Zac Stewart

Reputation: 23

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()?

Upvotes: 0

Views: 207

Answers (2)

micycle
micycle

Reputation: 3810

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();
        }
    }
});

Upvotes: 1

Kevin Workman
Kevin Workman

Reputation: 42176

You could do something like this:

cp5.addButton("appleEat");

And then define a function that calls the class:

void appleEat(int value){
  apple.eat();
}

Upvotes: 0

Related Questions