2017-05-30 56 views
0

因此,我試圖創建一個謎題遊戲,其中兩個人可以在同一個手機上進行直接對話。一切都很完美,除非你選擇了一個答案,那麼這個答案會如此快地產生下一個問題,而另一個人最終會碰到一個隨機答案。輸入/停止接收輸入後的延遲方法

例如,如果兩個玩家幾乎同時點擊按鈕,第一個和第二個之間的第二個點會產生一個新問題,而第二個玩家最終會針對不同的隨機問題點擊隨機答案。

任何人都可以幫助我理解如何在選擇答案後延遲下一個問題,並且在這種延遲發生時按鈕停止接收輸入?

代碼在下面,但實際上我只是需要知道頭向何方,不需要專門針對我的代碼。

感謝,

public class DuelMenu extends AppCompatActivity { 

Button answer1, answer2, answer3, answer4, playAgain, returnHome, answer1p2, answer2p2, answer3p2, answer4p2; 
TextView score, question, question2, score2; 
private MultiplyQuestions mQuestions = new MultiplyQuestions(); 
private String mAnswer; 
private int mScore = 0; 
private int mScore2 = 0; 
private int mQuestionLength = mQuestions.mQuestions.length; 
Random rng = new Random(); 
TextView mTextField, timer2; 
CountDownTimer gameTimer; 
boolean gameTimerIsRunning; 
TextView correctFade; 



public void startGame(){ 
    startGameTimer(); 

} 


public void startGameTimer(){ 
    gameTimer = new CountDownTimer(30000, 1000) { 


     @Override public void onTick(long millisUntilFinished) { 
      mTextField.setText("Time Remaining: " + millisUntilFinished/1000); 
      timer2.setText("Time Remaining: " + millisUntilFinished/1000); 
     } 

     @Override public void onFinish() { 
      gameTimerIsRunning = false; 
      gameOver(); 

     } 

    }; 
    gameTimer.start(); 
} 



@Override 
public void onBackPressed() { 
    moveTaskToBack(true); 
} 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    overridePendingTransition(R.anim.fadein, R.anim.fadeout); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.activity_duel_menu); 

    answer1 = (Button) findViewById(R.id.answer1); 
    answer2 = (Button) findViewById(R.id.answer2); 
    answer3 = (Button) findViewById(R.id.answer3); 
    answer4 = (Button) findViewById(R.id.answer4); 

    answer1p2 = (Button) findViewById(R.id.answer1p2); 
    answer2p2 = (Button) findViewById(R.id.answer2p2); 
    answer3p2 = (Button) findViewById(R.id.answer3p2); 
    answer4p2 = (Button) findViewById(R.id.answer4p2); 

    score = (TextView) findViewById(R.id.score); 
    score2 = (TextView) findViewById(R.id.score2); 
    question = (TextView) findViewById(R.id.question); 
    question2 = (TextView) findViewById(R.id.question2); 

    timer2 = (TextView) findViewById(R.id.timer2); 
    mTextField = (TextView) findViewById(R.id.timer); 
    correctFade = (TextView) findViewById(R.id.correctFade); 

    score.setText("Score " + mScore); 
    score2.setText("Score " + mScore2); 


    Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/Monster.ttf"); 
    score.setTypeface(custom_font); 
    score2.setTypeface(custom_font); 
    question.setTypeface(custom_font); 
    question2.setTypeface(custom_font); 
    mTextField.setTypeface(custom_font); 
    timer2.setTypeface(custom_font); 
    answer1p2.setTypeface(custom_font); 
    answer2p2.setTypeface(custom_font); 
    answer3p2.setTypeface(custom_font); 
    answer4p2.setTypeface(custom_font); 
    answer1.setTypeface(custom_font); 
    answer2.setTypeface(custom_font); 
    answer3.setTypeface(custom_font); 
    answer4.setTypeface(custom_font); 

    Button advanceToDuelFriend = (Button) findViewById(R.id.playAgain); 
    advanceToDuelFriend.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View agr0) { 
      Intent intent = new Intent(DuelMenu.this, DuelMenu.class); 
      startActivity(intent); 
     } 
    }); 

    Button advanceToHome = (Button) findViewById(R.id.returnHome); 
    advanceToHome.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View agr0) { 
      Intent intent = new Intent(DuelMenu.this, MainActivity.class); 
      startActivity(intent); 
     } 
    }); 

    final Animation fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fadeoutcorrect); 


    updateQuestion(rng.nextInt(mQuestionLength)); 
    startGame(); 


    answer1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (answer1.getText() == mAnswer) { 
       mScore++; 
       score.setText("Score " + mScore); 
       updateQuestion(rng.nextInt(mQuestionLength)); 
      } else { 
       mScore--; 
       score.setText("Score " + mScore); 
       updateQuestion(rng.nextInt(mQuestionLength)); 
      } 

     } 

    }); 

    answer2.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (answer2.getText() == mAnswer) { 
       mScore++; 
       score.setText("Score " + mScore); 
       updateQuestion(rng.nextInt(mQuestionLength)); 
      } else { 
       mScore--; 
       score.setText("Score " + mScore); 
       updateQuestion(rng.nextInt(mQuestionLength)); 
      } 

     } 
    }); 

    answer3.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (answer3.getText() == mAnswer) { 
       mScore++; 
       score.setText("Score " + mScore); 
       updateQuestion(rng.nextInt(mQuestionLength)); 
      } else { 
       mScore--; 
       score.setText("Score " + mScore); 
       updateQuestion(rng.nextInt(mQuestionLength)); 
      } 

     } 
    }); 

    answer4.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (answer4.getText() == mAnswer) { 
       mScore++; 
       score.setText("Score " + mScore); 
       updateQuestion(rng.nextInt(mQuestionLength)); 

      } else { 
       mScore--; 
       score.setText("Score " + mScore); 
       updateQuestion(rng.nextInt(mQuestionLength)); 
      } 

     } 
    }); 




    answer1p2.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (answer1p2.getText() == mAnswer) { 
       mScore2++; 
       score2.setText("Score " + mScore2); 
       updateQuestion(rng.nextInt(mQuestionLength)); 
      } else { 
       mScore2--; 
       score2.setText("Score " + mScore2); 
       updateQuestion(rng.nextInt(mQuestionLength)); 
      } 

     } 

    }); 

    answer2p2.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (answer2p2.getText() == mAnswer) { 
       mScore2++; 
       score2.setText("Score " + mScore2); 
       updateQuestion(rng.nextInt(mQuestionLength)); 
      } else { 
       mScore2--; 
       score2.setText("Score " + mScore2); 
       updateQuestion(rng.nextInt(mQuestionLength)); 
      } 

     } 
    }); 

    answer3p2.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (answer3p2.getText() == mAnswer) { 
       mScore2++; 
       score2.setText("Score " + mScore2); 
       updateQuestion(rng.nextInt(mQuestionLength)); 
      } else { 
       mScore2--; 
       score2.setText("Score " + mScore2); 
       updateQuestion(rng.nextInt(mQuestionLength)); 
      } 

     } 
    }); 

    answer4p2.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (answer4p2.getText() == mAnswer) { 
       mScore2++; 
       score2.setText("Score " + mScore2); 
       updateQuestion(rng.nextInt(mQuestionLength)); 

      } else { 
       mScore2--; 
       score2.setText("Score " + mScore2); 
       updateQuestion(rng.nextInt(mQuestionLength)); 
      } 

     } 
    }); 






} 

private void updateQuestion(int num) { 



    Animation fadeIn = AnimationUtils.loadAnimation(DuelMenu.this, R.anim.enter); 
    question.startAnimation(fadeIn); 

    fadeIn.setAnimationListener(new Animation.AnimationListener() { 
     @Override 
     public void onAnimationStart(Animation animation) { 
     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 
     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 
     } 

    }); 

    question.setText(mQuestions.getQuestion(num)); 
    answer1.setText(mQuestions.getChoice1(num)); 
    answer2.setText(mQuestions.getChoice2(num)); 
    answer3.setText(mQuestions.getChoice3(num)); 
    answer4.setText(mQuestions.getChoice4(num)); 

    question2.setText(mQuestions.getQuestion(num)); 
    answer1p2.setText(mQuestions.getChoice1(num)); 
    answer2p2.setText(mQuestions.getChoice2(num)); 
    answer3p2.setText(mQuestions.getChoice3(num)); 
    answer4p2.setText(mQuestions.getChoice4(num)); 


    mAnswer = mQuestions.getCorrectAnswer(num); 


} 



protected void gameOver() { 
    gameTimer.cancel(); 

    if (mScore > mScore2) { 
     Toast.makeText(this, "Player 1 Wins ", Toast.LENGTH_SHORT).show(); 
    } 

    if (mScore < mScore2) { 
     Toast.makeText(this, "Player 2 Wins", Toast.LENGTH_SHORT).show(); 
    } 

    if (mScore == mScore2) { 
     Toast.makeText(this, "IT'S A TIE!!", Toast.LENGTH_SHORT).show(); 
    } 

    playAgain = (Button) findViewById(R.id.playAgain); 
    playAgain.setVisibility(View.VISIBLE); 

    returnHome = (Button) findViewById(R.id.returnHome); 
    returnHome.setVisibility(View.VISIBLE); 

} 

public void quitToMenu(View view) { 
    gameTimer.cancel(); 
    startActivity(new Intent(getApplicationContext(), MainActivity.class)); 


} 



} 

回答

0

所以我想通了答案,我只是建一個帶有計時器的方法,並把它顯示計時器內部問題的方法。我還讓這些按鈕在勾選時不可見,並在完成時隱藏。

如果有更好的方法來做到這一點,請讓我知道,直到那時就足夠了!

我在下面使用的方法。

public void startGameTimer2(){ 
    answer1.setVisibility(View.INVISIBLE); 
    answer2.setVisibility(View.INVISIBLE); 
    answer3.setVisibility(View.INVISIBLE); 
    answer4.setVisibility(View.INVISIBLE); 

    answer1p2.setVisibility(View.INVISIBLE); 
    answer2p2.setVisibility(View.INVISIBLE); 
    answer3p2.setVisibility(View.INVISIBLE); 
    answer4p2.setVisibility(View.INVISIBLE); 
    gameTimer = new CountDownTimer(1000, 1000) { 


     @Override public void onTick(long millisUntilFinished) { 


     } 

     @Override public void onFinish() { 
      gameTimerIsRunning = false; 
      updateQuestion(rng.nextInt(mQuestionLength)); 
      answer1.setVisibility(View.VISIBLE); 
      answer2.setVisibility(View.VISIBLE); 
      answer3.setVisibility(View.VISIBLE); 
      answer4.setVisibility(View.VISIBLE); 

      answer1p2.setVisibility(View.VISIBLE); 
      answer2p2.setVisibility(View.VISIBLE); 
      answer3p2.setVisibility(View.VISIBLE); 
      answer4p2.setVisibility(View.VISIBLE); 
     } 

    }; 
    gameTimer.start(); 
}