暂停和恢复井字游戏赢得动画

时间:2018-07-30 13:54:55

标签: java android multithreading animation pause

我制作了井字游戏,并在最后添加了一个动画...每次爆竹动画从下往上播放时,按钮都会重叠(附上屏幕截图)。我想知道是否有一种简单的方法可以在使按钮可见之前在此处使用线程停止进程,以便可以轻松完成鞭炮动画。

代码如下:

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方法:

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);
}

完整代码:

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);
    }
}

1 个答案:

答案 0 :(得分:1)

好吧,如果我正确地阅读了这个问题,那么通常会使用一种方法,那就是创建一个睡眠方法。代码如下:

static void Sleep(int ms)
{
  try {
    Thread.sleep(ms);
   }catch (InterruptedException e) {
    e.printStackTrace();
   }
 }

如果我没看错问题,而我的解释是,您想将线程暂停一小段时间然后再恢复它。

要实现此目的,只需在使按钮可见之前调用Sleep({Time in milliseconds})