Created
June 22, 2017 22:51
-
-
Save KKorvin/c61b3d13b799a97fa8a2c88f76d06579 to your computer and use it in GitHub Desktop.
libGDX how to make sprite blinking x seconds
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 Blinker { | |
private float BLINK_TIME = 1f; | |
private int BLINKING_FRAMES = 4; | |
private boolean isBlinking; | |
private int blinkFrameCounter; | |
private float blinkTimer; | |
public Blinker() { | |
this.blinkTimer = 0; | |
this.blinkFrameCounter = 0; | |
this.isBlinking = false; | |
} | |
public boolean shouldBlink(float delta) { | |
if (isBlinking) { | |
blinkTimer += delta; | |
blinkFrameCounter++; | |
if (blinkTimer < BLINK_TIME) { | |
if (blinkFrameCounter % BLINKING_FRAMES == 0) { | |
return true; | |
} | |
} else { | |
blinkTimer = 0; | |
isBlinking = false; | |
} | |
} | |
return false; | |
} | |
public boolean isBlinking() { | |
return isBlinking; | |
} | |
public void setBlinking(boolean isBlinking) { | |
this.isBlinking = isBlinking; | |
} | |
} | |
//Usage | |
Blinker blinker= new Blinker(); | |
blinker.setBlinking(true); | |
//Add this to draw() method: | |
if (blinker.shouldBlink(delta)) | |
return; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment