What is the easiest way to fix this button class?
in
Programming Questions
•
9 months ago
hello all,
This is my button class. and call it using:
The problem is, sometimes you click it more than once. because at the stage = 5 you get the same button again.
I tried to reduce the frame rate but it does not solve my problem. If i reduce the frame rate to 10 than i can not press the button. i have to try it 2-3 times.
I know the problem is mousePressed
What can i do to solve this problem without chaanging alot of codes?
By the way, i can not change the location of button on the next stage. This is not the right solution.
Thanks alot
This is my button class. and call it using:
- stick.display(); // draw the button and text for stick
- stick.overButton(); // check if mouse is over for stick
- stick.pressedButton(); // check if mouse is clicked on stick
- if (stick.pressedButton()) { //
theplayer.stick();
stage = 5; // stage5_dealerTurn
}
The problem is, sometimes you click it more than once. because at the stage = 5 you get the same button again.
I tried to reduce the frame rate but it does not solve my problem. If i reduce the frame rate to 10 than i can not press the button. i have to try it 2-3 times.
I know the problem is mousePressed
What can i do to solve this problem without chaanging alot of codes?
By the way, i can not change the location of button on the next stage. This is not the right solution.
Thanks alot
- class Button {
- // declare variables
- color buttonColor; // button color when it is created
- color buttonColorOn; // button color when mouse is on it
- color textColor; // text color
- float buttonX; // x location of button
- float buttonY; // y location of button
- float buttonW; // button width
- float buttonH; // button height
- String buttonName; // button name
- boolean mouseOverButton = false;; // if mouse is over button
- boolean mousePressedButton = false;; // if mouse is pressed on button
- // Temporary variable assignment
- Button(color tempbuttonColor, color tempbuttonColorOn, color temptextColor, float tempbuttonX, float tempbuttonY, float tempbuttonW, float tempbuttonH, String tempbuttonName) {
- buttonColor = tempbuttonColor;
- buttonColorOn = tempbuttonColorOn;
- textColor = temptextColor;
- buttonX = tempbuttonX;
- buttonY = tempbuttonY;
- buttonW = tempbuttonW;
- buttonH = tempbuttonH;
- buttonName = tempbuttonName;
- }
- // The display() method is used to draw the button and text to the screen
- void display() {
- // if mouse is on a button then change the button color
- if (mouseOverButton) {
- fill(buttonColorOn);
- }
- else {
- fill(buttonColor);
- }
- rect(buttonX, buttonY, buttonW, buttonH); // draw button
- fill(textColor);
- text(buttonName, (buttonW/2)+buttonX-((buttonW/2)/1.5), (buttonH/2)+buttonY+((buttonH/2)/4));
- fill(buttonColor);
- }
- //##############################################################
- //##
- //## Returns true if mouse is over button, Returns false if mouse is not on the button
- //##
- //##############################################################
- boolean overButton() {
- if (mouseX >= buttonX && mouseX <= buttonX + buttonW && mouseY >= buttonY && mouseY <= buttonY + buttonH) {
- mouseOverButton = true;
- //Returns true back to main program
- return mouseOverButton;
- }
- else {
- mouseOverButton = false;
- //Returns false back to main program
- return mouseOverButton;
- }
- }
- //###############################################################
- //##
- //## Returns true if mouse is pressed, Returns false if mouse is not pressed
- //##
- //###############################################################
- boolean pressedButton() {
- if (mouseOverButton == true && mousePressed) {
- mousePressedButton = true;
- return mousePressedButton;
- }
- else {
- mousePressedButton = false;
- return mousePressedButton;
- }
- }
- }
1