I’m currently making a small game and I wanted to implement different screens so I would have a Main Screen, a Settings Screen, and a Game Screen. So first I wanted to set up the main screen and it would contain some buttons. I wrote a custom Button class for easier management and a custom class for the main screen.
My problem is that I want to do a detection for when the button has been clicked using the mouse. In my button class, I can detect the click (and also check if it even clicked inside of the buttons hitbox) but I don’t know how I would pass the information about the click on to the main screen.
So I was thinking about making a custom event: ButtonClick. But I have no knowledge of how to create custom events and how to trigger it. So if anyone could help me with figuring out how to make a custom event then that would be much appreciated.
Thank you.
// Menu Class
class MenuScreen extends Screen {
ArrayList buttons = new ArrayList<>();
MenuScreen() {
buttons.add("Test", 35, width / 2, height / 2, 100, 50);
}
void display() {
//Display layout
}
}
// Button Class
class Button {
float dx, dy, x, y, sizeX, sizeY;
String text;
int textSize;
boolean isActive;
Button(String text, int textSize, float x, float y, float sizeX, float sizeY) {
this.text = text;
this.textSize = textSize;
this.x = x;
this.dx = dx - sizeX / 2;
this.y = y;
this.dy = dy - sizeY / 2;
this.sizeX = sizeX;
this.sizeY = sizeY;
}
// Detect button click
void mouseReleased() {
if (this.isActive)
if (this.dx <= mouseX && this.dx + this.sizeX >= mouseX)
print("Clicked");
// Call custom event here
}
}
There is a bit of a challenge to implement your class. To draw on the sketch from your class, you need to pass a reference to your parent sketch right into your class. This can be explained a bit better after you show your code of how you integrate this code with setup() and draw() in your code.
For the time being, check this next post about states: Need Help for Game Over / Timer
With states, you can change the page that is displayed. In the first state, you show the intro page, next state you show the menu page, next stage is the game and another state could be game pause or game over.
Thank you for your reply. The shown code is in the same class as the setup() and draw() methods. But you’re saying something about passing and referring my main sketch? So are you saying that the custom event would need to be it’s own sketch or?
You do not need to do this though. You could just use states and use the global environment that Processing offers you. If you want to create your own class, then you need to have a look at that link.
Yea I’ve seen the Library Basic thing before but I just didn’t know if the code would need to be in a separate class (a new file) or what I should do with it. And then again, I don’t know how I would pass my custom button class into the event because I want to call the event so it would be something like “void buttonClick() {” and then be able to get the location of the button and such.
When you implement your setup and draw, then you will see how everything “clicks”. That is, if you go ahead to use classes. If you want a simpler way, you can work in the global scope.
You need to provide some runnable code. You do not need to implement the event part, but you need to draw your button (a button) or your menu screen, since those are the components you are proposing in your initial post. I could provide some guidance after you do this.
If you want to do it by yourself then my answer won’t be really useful but have you thought about using a library? You can found several on this page at the GUI section.
Okay guys so I figured out that if you call the mousePressed event from an other class than the main class with the setup and draw function it will only run once so the custom event thing wouldn’t have worked anyway. But I just changed up a few things and my code is now working.