我想清除一個動畫(這是一個三行閃爍的按鈕在一行')與按下我分配給這個遊戲,清除董事會的按鈕通過下面顯示的「clearGame」方法。然而,在玩遊戲時(它還沒有設置按鈕,稍後會做),它會正確顯示3線閃爍(假設用按鈕1,4,7),當我調用「clearGame」方法時它重置了電路板。但是,由於某種原因,如果我用另一組按鈕贏得比賽(例如我用按鈕3,6,9贏得比賽),它會顯示兩組按鈕(比如說3,6,9,& 1,4,7 )閃爍,而不是隻有一組。這裏是主遊戲的活動:清除動畫從井字遊戲/ Nack&Cross在Android
package com.example.noughtsandcrosses;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
public class NoughtsAndCrossesXActivity extends Activity implements OnClickListener {
Button[] buttons = new Button[10];
int[] squares = new int[10];
Button newGame;
Button exitGame;
TextView WinGame;
Animation WinningAnim;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.noughts_and_croses);
buttons[1] = (Button) findViewById(R.id.one);
buttons[2] = (Button) findViewById(R.id.two);
buttons[3] = (Button) findViewById(R.id.three);
buttons[4] = (Button) findViewById(R.id.four);
buttons[5] = (Button) findViewById(R.id.five);
buttons[6] = (Button) findViewById(R.id.six);
buttons[7] = (Button) findViewById(R.id.seven);
buttons[8] = (Button) findViewById(R.id.eight);
buttons[9] = (Button) findViewById(R.id.nine);
newGame = (Button) findViewById(R.id.new_game);
newGame.setOnClickListener(this);
WinGame = (TextView) findViewById(R.id.win_game);
exitGame = (Button) findViewById(R.id.exit_game);
exitGame.setOnClickListener(this);
for (int i = 1; i <= 9; i++){
buttons[i].setOnClickListener(this);
clearGame();
}
WinningAnim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.winning_animation);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login_page, menu);
return true;
}
public void clearGame(){
for (int i = 1; i <= 9; i++){
buttons[i].setText("");
buttons[i].setEnabled(true);
squares[i] = 0;
}
WinGame.setText("");
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.one:
makeMove(1);
respond();
break;
case R.id.two:
makeMove(2);
respond();
break;
case R.id.three:
makeMove(3);
respond();
break;
case R.id.four:
makeMove(4);
respond();
break;
case R.id.five:
makeMove(5);
respond();
break;
case R.id.six:
makeMove(6);
respond();
break;
case R.id.seven:
makeMove(7);
respond();
break;
case R.id.eight:
makeMove(8);
respond();
break;
case R.id.nine:
makeMove(9);
respond();
break;
case R.id.new_game:
System.out.println("newgame");
clearGame();
break;
case R.id.exit_game:
System.out.println("exitgame");
//exitGameToMenu();
Intent a = new Intent (this, MenuActivity.class);
startActivity(a);
break;
}
}
public void CheckIfPlayerWon (int i) {
i = 1;
if (squares[1] == i && squares[2] == i && squares[3] == i){
WinGame.setText("You have won the game! 1");
buttons[1].startAnimation(WinningAnim);
buttons[2].startAnimation(WinningAnim);
buttons[3].startAnimation(WinningAnim);
buttons[4].setEnabled(false);
buttons[5].setEnabled(false);
buttons[6].setEnabled(false);
buttons[7].setEnabled(false);
buttons[8].setEnabled(false);
buttons[9].setEnabled(false);
}
else if (squares[4] == i && squares[5] == i && squares[6] == i){
WinGame.setText("You have won the game! 2");
buttons[1].setEnabled(false);
buttons[2].setEnabled(false);
buttons[3].setEnabled(false);
buttons[4].startAnimation(WinningAnim);
buttons[5].startAnimation(WinningAnim);
buttons[6].startAnimation(WinningAnim);
buttons[7].setEnabled(false);
buttons[8].setEnabled(false);
buttons[9].setEnabled(false);
}
else if (squares[7] == i && squares[8]== i && squares[9] == i){
WinGame.setText("You have won the game! 3");
buttons[1].setEnabled(false);
buttons[2].setEnabled(false);
buttons[3].setEnabled(false);
buttons[4].setEnabled(false);
buttons[5].setEnabled(false);
buttons[6].setEnabled(false);
buttons[7].startAnimation(WinningAnim);
buttons[8].startAnimation(WinningAnim);
buttons[9].startAnimation(WinningAnim);
}
else if (squares[1]== i && squares[4] == i && squares[7] == i){
WinGame.setText("You have won the game! 4");
buttons[1].startAnimation(WinningAnim);
buttons[2].setEnabled(false);
buttons[3].setEnabled(false);
buttons[4].startAnimation(WinningAnim);
buttons[5].setEnabled(false);
buttons[6].setEnabled(false);
buttons[7].startAnimation(WinningAnim);
buttons[8].setEnabled(false);
buttons[9].setEnabled(false);
}
else if (squares[2] == i && squares[5] == i && squares[8] == i){
WinGame.setText("You have won the game! 5");
buttons[1].setEnabled(false);
buttons[2].startAnimation(WinningAnim);
buttons[3].setEnabled(false);
buttons[4].setEnabled(false);
buttons[5].startAnimation(WinningAnim);
buttons[6].setEnabled(false);
buttons[7].setEnabled(false);
buttons[8].startAnimation(WinningAnim);
buttons[9].setEnabled(false);
}
else if (squares[3] == i && squares[6] == i && squares[9] == i){
WinGame.setText("You have won the game! 6");
buttons[1].setEnabled(false);
buttons[2].setEnabled(false);
buttons[3].startAnimation(WinningAnim);
buttons[4].setEnabled(false);
buttons[5].setEnabled(false);
buttons[6].startAnimation(WinningAnim);
buttons[7].setEnabled(false);
buttons[8].setEnabled(false);
buttons[9].startAnimation(WinningAnim);
}
else if (squares[1] == i && squares[5] == i && squares[9] == i){
WinGame.setText("You have won the game! 7");
buttons[1].startAnimation(WinningAnim);
buttons[2].setEnabled(false);
buttons[3].setEnabled(false);
buttons[4].setEnabled(false);
buttons[5].startAnimation(WinningAnim);
buttons[6].setEnabled(false);
buttons[7].setEnabled(false);
buttons[8].setEnabled(false);
buttons[9].startAnimation(WinningAnim);
}
else if (squares[3] == i && squares[5] == i && squares[7] == i){
WinGame.setText("You have won the game! 8");
buttons[1].setEnabled(false);
buttons[2].setEnabled(false);
buttons[3].startAnimation(WinningAnim);
buttons[4].setEnabled(false);
buttons[5].startAnimation(WinningAnim);
buttons[6].setEnabled(false);
buttons[7].startAnimation(WinningAnim);
buttons[8].setEnabled(false);
buttons[9].setEnabled(false);
}
}
public void CheckIfPlayerLost (int i) {
i = 2;
if (squares[1] == i && squares[2] == i && squares[3] == i){
WinGame.setText("You have lost the game! 1");
buttons[1].setEnabled(false);
buttons[2].setEnabled(false);
buttons[3].setEnabled(false);
buttons[4].setEnabled(false);
buttons[5].setEnabled(false);
buttons[6].setEnabled(false);
buttons[7].setEnabled(false);
buttons[8].setEnabled(false);
buttons[9].setEnabled(false);
}
else if (squares[4] == i && squares[5] == i && squares[6] == i){
WinGame.setText("You have lost the game! 2");
buttons[1].setEnabled(false);
buttons[2].setEnabled(false);
buttons[3].setEnabled(false);
buttons[4].setEnabled(false);
buttons[5].setEnabled(false);
buttons[6].setEnabled(false);
buttons[7].setEnabled(false);
buttons[8].setEnabled(false);
buttons[9].setEnabled(false);
}
else if (squares[7] == i && squares[8]== i && squares[9] == i){
WinGame.setText("You have lost the game! 3");
buttons[1].setEnabled(false);
buttons[2].setEnabled(false);
buttons[3].setEnabled(false);
buttons[4].setEnabled(false);
buttons[5].setEnabled(false);
buttons[6].setEnabled(false);
buttons[7].setEnabled(false);
buttons[8].setEnabled(false);
buttons[9].setEnabled(false);
}
else if (squares[1]== i && squares[4] == i && squares[7] == i){
WinGame.setText("You have lost the game! 4");
buttons[1].setEnabled(false);
buttons[2].setEnabled(false);
buttons[3].setEnabled(false);
buttons[4].setEnabled(false);
buttons[5].setEnabled(false);
buttons[6].setEnabled(false);
buttons[7].setEnabled(false);
buttons[8].setEnabled(false);
buttons[9].setEnabled(false);
}
else if (squares[2] == i && squares[5] == i && squares[8] == i){
WinGame.setText("You have lost the game! 5");
buttons[1].setEnabled(false);
buttons[2].setEnabled(false);
buttons[3].setEnabled(false);
buttons[4].setEnabled(false);
buttons[5].setEnabled(false);
buttons[6].setEnabled(false);
buttons[7].setEnabled(false);
buttons[8].setEnabled(false);
buttons[9].setEnabled(false);
}
else if (squares[3] == i && squares[6] == i && squares[9] == i){
WinGame.setText("You have lost the game! 6");
buttons[1].setEnabled(false);
buttons[2].setEnabled(false);
buttons[3].setEnabled(false);
buttons[4].setEnabled(false);
buttons[5].setEnabled(false);
buttons[6].setEnabled(false);
buttons[7].setEnabled(false);
buttons[8].setEnabled(false);
buttons[9].setEnabled(false);
}
else if (squares[1] == i && squares[5] == i && squares[9] == i){
WinGame.setText("You have lost the game! 7");
buttons[1].setEnabled(false);
buttons[2].setEnabled(false);
buttons[3].setEnabled(false);
buttons[4].setEnabled(false);
buttons[5].setEnabled(false);
buttons[6].setEnabled(false);
buttons[7].setEnabled(false);
buttons[8].setEnabled(false);
buttons[9].setEnabled(false);
}
else if (squares[3] == i && squares[5] == i && squares[7] == i){
WinGame.setText("You have lost the game! 8");
buttons[1].setEnabled(false);
buttons[2].setEnabled(false);
buttons[3].setEnabled(false);
buttons[4].setEnabled(false);
buttons[5].setEnabled(false);
buttons[6].setEnabled(false);
buttons[7].setEnabled(false);
buttons[8].setEnabled(false);
buttons[9].setEnabled(false);
}
}
public void makeMove(int i) {
buttons[i].setText("X");
buttons[i].setEnabled(false);
squares[i] = 1;
CheckIfPlayerWon(i);
}
public void respond() {
for (int i = 1; i <= 9; i++) {
if (buttons[i].isEnabled()) {
buttons[i].setText("O");
buttons[i].setEnabled(false);
squares[i] = 2;
CheckIfPlayerLost(i);
break;
}
}
}
}
有誰知道我要去哪裏錯?我試圖在clearGame方法中輸入一行(比如說「WinningAnim.clearAnimation()」)來重置動畫,但是當我開始時會導致遊戲崩潰,但是當我刪除該行時它很好(除了動畫問題)
謝謝,但我需要得到這個星期一之前完成,並重新寫入精簡找出勝利者和失敗者是方法。雖然對於清晰的動畫方法,我該如何去執行它? – user3350982
當你循環時,你會將它插入到你的清晰遊戲方法中,只需將它放入:'buttons [i] .clearAnimation()'。嘗試這個。 – keeslinp