We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I started a thread some time ago asking for neat ways to code buttons (https://forum.processing.org/two/discussion/13157/what-s-your-favourite-way-to-code-a-button#latest)
And There were two great answers that used method()
Which I didn't even knew existed.
But is there a way to use method()
or something similar to access a method inside a class?
Answers
https://Processing.org/reference/thread_.html
Ok, the only problem with that is that if I have 50 different buttons I will have to have 50 functions in the first tab, which is annoying and a really roundabout way of doing it...
But if there is no other way I'l just have to work with it :)
If Button is some class, you'd just have to figure out which 1 was clicked at.
Once found, call its class method and you're done.
However, if each Button is tasked to do a diff. thing, you're gonna need a diff. technique too.
Let's name that method action(). Let's say you've got 3 Button objects.
1st Button is tasked to draw a rect(). 2nd an ellipse() and 3rd a triangle().
At the moment we instantiate each of those 3, we
@Override
their action() method.Even though all those 3 got a method called action(), each action()'s gonna act differently from each other! :ar!
Pay specially close attention within class Button, the
abstract
method action() demands to be implemented lest the whole class can't be instantiated!The following example is a modification of this online sketch below:
http://studio.ProcessingTogether.com/sp/pad/export/ro.9eDRvB4LRmLrr
And from this old forum thread below:
https://forum.Processing.org/two/discussion/558/creating-a-next-page-button
Alternatively, give each button an unique id
When clicked, read the id of the clicked Button
write in a new tab a function
doCommand()
and pass it the idNow
Ok, GoToLoop, so you instantiate the buttons in a new method/function and when you do that, you also defines what the method action() is going to do? That's actually pretty neat. But you have to instantiate it in a new method/function and not in setup?
Thanks a lot for the help guys, this makes things much easier.
It's neat
It's better in a extra function but also possible in setup()
createButtons() is called from setup().
Once createButtons() is finished, it goes back to setup(), which has called it.
Of course you can pick createButtons()'s content and dump it all into setup().
But that's not very modular & it's against "separation of concerns" (SoC):
https://en.Wikipedia.org/wiki/Separation_of_concerns