I have made tic tac toe and have added an animation in the end... Every time the firecracker animation plays bottom to up it is overlapped by the button (attached screenshot). I want to know if there is a simple way to use threading over here to stop the process before making the buttons visible so that the firecracker animation can easily finish.
Here's the code:
for (int[] winningPosition : winningPositions)
{
if (gameState[winningPosition[0]] == gameState[winningPosition[1]] && gameState[winningPosition[1]] == gameState[winningPosition[2]] && gameState[winningPosition[0]] != 2) {
// Someone has won!
gameActive = false;
String winner = "";
if (activePlayer == 0) {
winner = "O";
} else {
winner = "X";
}
Button playAgainButton = (Button) findViewById(R.id.playAgainButton);
TextView winnerTextView = (TextView) findViewById(R.id.winnerTextView);
winnerTextView.setText(winner + " has won!");
ImageView firecracker = (ImageView) findViewById(R.id.firecracker);
firecracker.setTranslationY(-1000);
firecracker.setVisibility(View.VISIBLE);
firecracker.animate().translationYBy(-3000).setDuration(1000);
firecracker.setTranslationY(4000);
playAgainButton.setVisibility(View.VISIBLE);
winnerTextView.setVisibility(View.VISIBLE);
}
}
Onclick method for the play again button:
public void reset(View view)
{
ImageView firecracker = (ImageView) findViewById(R.id.firecracker);
firecracker.setVisibility(View.INVISIBLE);
Button playAgainButton = (Button) findViewById(R.id.playAgainButton);
TextView winnerTextView = (TextView) findViewById(R.id.winnerTextView);
playAgainButton.setVisibility(View.INVISIBLE);
winnerTextView.setVisibility(View.INVISIBLE);
}
Full code:
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.security.spec.InvalidKeySpecException;
public class MainActivity extends AppCompatActivity {
// 0: X, 1: O, 2: Empty
int [] gameState = {2, 2, 2, 2, 2, 2, 2, 2, 2};
int [][] winningPositions = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0,3,6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6}};
boolean gameActive = true;
int activePlayer = 0;
public void dropIn(View view)
{
ImageView counter = (ImageView) view;
int tappedCounter = Integer.parseInt(counter.getTag().toString());
if (gameState[tappedCounter] == 2 && gameActive)
{
gameState[tappedCounter] = activePlayer;
counter.setTranslationY(-2000);
counter.setImageResource(R.drawable.o);
counter.animate().translationYBy(2000).setDuration(300);
if (activePlayer == 0) {
counter.setImageResource(R.drawable.x);
activePlayer = 1;
} else {
counter.setImageResource(R.drawable.o);
activePlayer = 0;
}
boolean emptySquare = false;
for (int squareState : gameState) {
if (squareState == 2) {
emptySquare = true;
break;
}
}
if (!emptySquare && gameActive) {
// Game is a draw
gameActive = true;
Button playAgainButton = (Button) findViewById(R.id.playAgainButton);
TextView winnerTextView = (TextView) findViewById(R.id.winnerTextView);
winnerTextView.setText("Game is a draw...");
playAgainButton.setVisibility(View.VISIBLE);
winnerTextView.setVisibility(View.VISIBLE);
}
for (int[] winningPosition : winningPositions)
{
if (gameState[winningPosition[0]] == gameState[winningPosition[1]] && gameState[winningPosition[1]] == gameState[winningPosition[2]] && gameState[winningPosition[0]] != 2) {
// Someone has won!
gameActive = false;
String winner = "";
if (activePlayer == 0) {
winner = "O";
} else {
winner = "X";
}
Button playAgainButton = (Button) findViewById(R.id.playAgainButton);
TextView winnerTextView = (TextView) findViewById(R.id.winnerTextView);
winnerTextView.setText(winner + " has won!");
ImageView firecracker = (ImageView) findViewById(R.id.firecracker);
firecracker.setTranslationY(-1000);
firecracker.setVisibility(View.VISIBLE);
firecracker.animate().translationYBy(-3000).setDuration(1000);
firecracker.setTranslationY(4000);
playAgainButton.setVisibility(View.VISIBLE);
winnerTextView.setVisibility(View.VISIBLE);
}
}
}
}
public void reset(View view)
{
ImageView firecracker = (ImageView) findViewById(R.id.firecracker);
firecracker.setVisibility(View.INVISIBLE);
Button playAgainButton = (Button) findViewById(R.id.playAgainButton);
TextView winnerTextView = (TextView) findViewById(R.id.winnerTextView);
playAgainButton.setVisibility(View.INVISIBLE);
winnerTextView.setVisibility(View.INVISIBLE);
android.support.v7.widget.GridLayout gridLayout = (android.support.v7.widget.GridLayout) findViewById(R.id.gridLayout);
for (int i = 0; i < gridLayout.getChildCount(); i++)
{
ImageView counter = (ImageView) gridLayout.getChildAt(i);
counter.setImageDrawable(null);
}
for (int i = 0; i < gameState.length; i++)
{
gameState[i] = 2;
}
activePlayer = 0;
gameActive = true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Well, if I read the question right theres one method I usually use and that's to create a sleep method.. The code goes as follows:
static void Sleep(int ms)
{
try {
Thread.sleep(ms);
}catch (InterruptedException e) {
e.printStackTrace();
}
}
If I read the question right that is, and my interpretation is that you want to pause the thread for a short period of time and then resume it.
In order to implement this, just call Sleep({Time in milliseconds}) before you make the buttons visible