Last active
December 7, 2020 14:35
-
-
Save codingminecraft/66919233a46544b3f40c457aa438d052 to your computer and use it in GitHub Desktop.
Mouse Click Debounce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MoneyButton extends GameObject{ | |
public Sprite buttonClickable; | |
public Sprite buttonClicked; | |
public int money = 0; | |
// Add a debounce time so that you can wait before registering the next click | |
private double mouseDebounceTime = 0.1; | |
private double mouseDebounceTimeLeft = 0.1; | |
public enum ButtonStatus { | |
Clickable, | |
Clicked; | |
} | |
public ButtonStatus buttonStatus; | |
public MoneyButton(String objectName, Transform transform) { | |
super(objectName, transform); | |
buttonClickable = new Sprite("Assets\\Images\\Texture\\Items\\CoinButton.png"); | |
buttonClicked = new Sprite("Assets\\Images\\Texture\\Items\\CoinButtonClicked.png"); | |
buttonStatus = ButtonStatus.Clickable; | |
} | |
@Override | |
public void update(double delta) { | |
// Subtract delta from the mouseDebounceTimeLeft so that we track how much time | |
// is left til we can register another mouse 'press' | |
mouseDebounceTimeLeft -= delta; | |
// Check if mouseDebounceTimeLeft <= 0 so that we know 0.1 seconds has passed | |
if (Game.mouseListener.mousePressed == true && mouseDebounceTimeLeft <= 0) { | |
buttonStatus = ButtonStatus.Clicked; | |
money++; | |
// Reset mouseDebounceTimeLeft so that it tracks it again | |
mouseDebounceTimeLeft = mouseDebounceTime; | |
} else if(Game.mouseListener.mousePressed == false) { | |
buttonStatus = ButtonStatus.Clickable; | |
} | |
} | |
@Override | |
public void render(Graphics g) { | |
if(buttonStatus == ButtonStatus.Clickable) { | |
g.drawImage(buttonClickable.image,(int)this.transform.postion.x, (int)this.transform.postion.y,300, 300, null); | |
} else if(buttonStatus == ButtonStatus.Clicked) { | |
g.drawImage(buttonClicked.image,(int)this.transform.postion.x, (int)this.transform.postion.y,300, 300, null); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment