我做了一個3級的遊戲,簡單,正常,很難。如何停止計時器在其他級別顯示?
容易擁有300秒鐘,然後timeup活動顯示了, 正常的有600秒, 難有900秒
例如,我選擇容易,但我輸了,所以我又回到了選擇我會採取什麼樣的水平下一個。我選擇了正常,但計時器頁面很容易顯示出來。
我該如何阻止TIMER在我的其他級別彈出。
我失去了一些東西的水平
活動:
public class EasyAct extends AppCompatActivity {
Button b1, b2, b3, b4;
TextView tv1, tv2, tv3, tv4;
int score =0;
TriviaQuizHelper1 triviaQuizHelper1;
TriviaQuestion1 currentQuestion;
List<TriviaQuestion1> list;
int qid = 0;
int level = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_easy);
tv1 = (TextView) findViewById(R.id.triviaQuestion);
tv2 = (TextView) findViewById(R.id.triviTimer);
b1 = (Button) findViewById(R.id.buttonA);
b2 = (Button) findViewById(R.id.buttonB);
b3 = (Button) findViewById(R.id.buttonC);
b4 = (Button) findViewById(R.id.buttonD);
tv3 = (TextView) findViewById(R.id.level);
tv4 = (TextView) findViewById(R.id.score);
CountDownTimer countDownTimer = new CountDownTimer(300 * 1000, 1000) {
@Override
public void onTick(long l) {
tv2.setText("Timer:" + l/1000 + " " + "sec");
}
@Override
public void onFinish() {
tv2.setText("TIME OUT");
Intent intent = new Intent(EasyAct.this, timeup.class);
startActivity(intent);
}
};
countDownTimer.start();
triviaQuizHelper1 = new TriviaQuizHelper1(this);
SQLiteDatabase sqLiteDatabase = triviaQuizHelper1.getWritableDatabase();
triviaQuizHelper1.allQuestion1();
list = triviaQuizHelper1.getAllQuestion();
currentQuestion = list.get(qid);
view();
}
public void view() {
tv1.setText(currentQuestion.getQuestion());
b1.setText(" "+currentQuestion.getOpta());
b2.setText(" "+currentQuestion.getOptb());
b3.setText(" "+currentQuestion.getOptc());
b4.setText(" "+currentQuestion.getOptd());
tv3.setText("Level:"+level);
tv4.setText("Score:"+score);
}
public void ButtonA(View view) {
if (b1.getText().toString().equals(" "+currentQuestion.getAnswer())){
score+=300;
if(qid<19){
qid++;
currentQuestion = list.get(qid);
view();
}else{
Intent intent=new Intent(EasyAct.this,ResultWon.class);
startActivity(intent);
finish();
}
}
else {
Intent intent=new Intent(EasyAct.this,ResultPlayAgain.class);
Bundle b=new Bundle();
b.putInt("xscore",score);
intent.putExtras(b);
startActivity(intent);
finish();
}
}
public void ButtonB(View view) {
if (b2.getText().toString().equals(" "+currentQuestion.getAnswer())){
score+=300;
if(qid<19){
qid++;
currentQuestion = list.get(qid);
view();
}else{
Intent intent=new Intent(EasyAct.this,ResultWon.class);
startActivity(intent);
finish();
}
}
else {
Intent intent=new Intent(EasyAct.this,ResultPlayAgain.class);
Bundle b=new Bundle();
b.putInt("xscore",score);
intent.putExtras(b);
startActivity(intent);
finish();
}
}
public void ButtonC(View view) {
if (b3.getText().toString().equals(" "+currentQuestion.getAnswer())){
score+=300;
if(qid<19){
qid++;
currentQuestion = list.get(qid);
view();
}else{
Intent intent=new Intent(EasyAct.this,ResultWon.class);
startActivity(intent);
finish();
}
}
else {
Intent intent=new Intent(EasyAct.this,ResultPlayAgain.class);
Bundle b=new Bundle();
b.putInt("xscore",score);
intent.putExtras(b);
startActivity(intent);
finish();
}
}
public void ButtonD(View view) {
if (b4.getText().toString().equals(" "+currentQuestion.getAnswer())){
score+=300;
if(qid<19){
qid++;
currentQuestion = list.get(qid);
view();
}else{
Intent intent=new Intent(EasyAct.this,ResultWon.class);
startActivity(intent);
finish();
}
}
else {
Intent intent=new Intent(EasyAct.this,ResultPlayAgain.class);
Bundle b=new Bundle();
b.putInt("xscore",score);
intent.putExtras(b);
startActivity(intent);
finish();
}
}
@Override
public void onBackPressed() {
Intent a = new Intent(Intent.ACTION_MAIN);
a.addCategory(Intent.CATEGORY_HOME);
a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(a);
finish();
}
}
我只看到被傳遞到定時器300秒,做你的其他activitys有不同數量硬編碼?我猜你只需要將定時器聲明爲每個類的全局變量,那麼當你丟失或重新開始時,你可以調用timer.stop()方法 – buradd
我將在代碼I中鍵入timer.stop()發佈 – pauro
請參閱:https://stackoverflow.com/help/someone-answers – c0der