Skip to content

Instantly share code, notes, and snippets.

@sdocy
Last active March 3, 2018 09:47
Show Gist options
  • Save sdocy/f195beeb08a29827417adb324e21c83a to your computer and use it in GitHub Desktop.
Save sdocy/f195beeb08a29827417adb324e21c83a to your computer and use it in GitHub Desktop.
Implementing multiple, successive, synchronous delays in Java on Android
// Implementing synchronous delays at specific points in the game to, for example, slow down card dealing so that the
// user can follow the action, was tricky. In Unity, I had to learn how to mix asynchronous coroutines with synchronous, event-driven
// code. Java presented a number of approaches, but none which allowed for multiple, successive, synchronous delays. Java has :
//
// - Thread.Sleep() and SystemClock.Sleep(), which do exactly what I want, but they can't be used on the main UI thread, or
// updates to the screen will not take place until after the sleep, even if they are initiated before the sleep.
// Ok, well, Java includes handlers and runnables for triggering the execution of code after a certain delay. But as far as I
// can tell, there is no way to make the delays synchronous with respect to the thread that spawned them, so in order to get
// multiple, delayed events, you would need to implement a complex, unmaintainable chain of runnables.
// - So I learned how to create a new thread in Java. I figured that would allow me to use Thread.Sleep() to get the delays
// I wanted. And I was correct, EXCEPT...you can only update UI elements (buttons, TextViews, ImageViews, etc.) from the main
// UI thread. Ahhhhhh....seemed like every course I investigated kept running into one gotcha or another.
// - I read that Looper threads may be able to update screen elements, but I couldn't find an explanation of them that was clear.
// But then I saw runOnUiThread() which sounded like it would execute the specified runnable on the main UI thread, which
// could then update screen elements. I tried creating a new thread, using Thread.Sleep() prior to an action that I wanted to
// delay, and then using runOnUiThread() to give the task to the UI thread if it was something that updated the screen. It all
// worked beautifully, and didn't turn out quite as cumbersome as it sounds.
// - At some point, I will go back and try to figure out Looper threads
//
// called when the user hits the "DEAL" button
public void deal(View view) {
// disable buttons before creating a thread because we don't want a delay before doing it
// and we need it to be excuted on the UI thread
disableAll();
// reset hand state for a new hand
init();
// deduct bet amount from player's cash
subtractBet();
// create a new thread to execute all the code that I want to introduce
// synchronous delays between
new Thread() {
public void run() {
doDelay(DEAL_DELAY);
// deal player their first card
// anything that updates on-screen elements must be run on the UI thread
runOnUiThread(new Runnable() {
@Override
public void run() {
Deck.shuffleDeck();
//Deck.printDeck();
}
});
doDelay(DEAL_DELAY);
// deal player their first card
// anything that updates on-screen elements must be run on the UI thread
runOnUiThread(new Runnable() {
@Override
public void run() {
dealCardToPlayer(thePlayer);
}
});
doDelay(DEAL_DELAY);
// deal first card to dealer
// anything that updates on-screen elements must be run on the UI thread
runOnUiThread(new Runnable() {
@Override
public void run() {
dealCardToPlayer(theDealer);
}
});
doDelay(DEAL_DELAY);
// deal player their second card
// anything that updates on-screen elements must be run on the UI thread
runOnUiThread(new Runnable() {
@Override
public void run() {
dealCardToPlayer(thePlayer);
}
});
doDelay(DEAL_DELAY);
// deal hole card to dealer
// anything that updates on-screen elements must be run on the UI thread
runOnUiThread(new Runnable() {
@Override
public void run() {
dealHoleCardToDealer();
// anyone get dealt blackjack?
if (!checkForBJ()) {
enableHitStand();
if ((thePlayer.cardTotal == 9) || (thePlayer.cardTotal == 10) || (thePlayer.cardTotal == 11)) {
if (playerCash >= playerBet) {
enableDoubleDown();
}
}
}
}
});
}
}.start();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment