I’m trying to show a timer to the user that counts down from 3 to 0. This timer should be made visible when a user drags an object in a certain position and should be stopped (and resetted) when the user drags the object out of that position..
For now, I’m working with Log.d, the actual displaying of the timer, I’ll deal with later.
So far, I’ve got (non-working code) :
public void setX(float x) {
isCheckOut = false;
// if basket is further than slotmargin
if (x + width/2 > SlotManager.SlotmarginX) {
// if basket is NOT in CheckOut area
if (this.Y < MainGamePanel.height - 80 - height/2){
x = SlotManager.SlotmarginX - width/2;
} else {
// if basket IS in CheckOut area
x = MainGamePanel.width - width/2;
isCheckOut = true;
GameManager.CheckOut();
}
}
this.X = x;
}
And, in the GameManager :
public static void CheckOut() {
Thread t = new Thread(new Runnable() {
public void run() {
while (basket.isCheckOut == true){
for (int i=3;i>3;i--){
final Handler handler = new Handler();
Timer t = new Timer();
t.schedule(new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
Log.d("", "Countdown " + i);
}
});
}
}, 2000);
}
}
}
});
t.start();
}
I feel like I’m getting somewhere, but can’t wrap my head around the loop-timer combination. The timer should stop when the isCheckOut == false. Now, I’m thinking I start the timer over-and-over… AND I cannot use the int “i” to display the current countdown int, because it’s not final?
Help is deeply appreciated!
Better use this approach:
To start timer use: