2014-03-30 50 views
0

我有一個遊戲,如果用戶觸摸錯誤的按鈕,他會去高分頁面,如果他點擊正確的一個,他會進入下一個關卡。我想要做的是如果用戶在1.5秒內完全沒有任何東西(快節奏的遊戲),那麼它會自動回到scores.class活動。我是編程新手,所以任何幫助!謝謝。如何設置一個android活動的時間限制

回答

0

這會給你一個想法:

private MainActivity context; 
private CountDownTimer countDownTimer; 
public boolean timerStopped; 


/** Called when the activity is first created. */ 
@Override protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    context = this; 

    startTimer(); 

    // method looks at users choice, example 
    /* if (answer == true){ 
     stopTimer(); 
     // go to next question and start timer again.. 
    } 
    else{ 
     // do something 
    } 
    */ 
} 

/** Starts the timer **/ 
public void startTimer() { 
    setTimerStartListener(); 
    timerStopped = false; 
} 

/** Stop the timer **/ 
public void stopTimer() { 
    countDownTimer.cancel(); 
    timerStopped = true; 
} 

/** Timer method: CountDownTimer **/ 
private void setTimerStartListener() { 
    // will be called at every 1500 milliseconds i.e. every 1.5 second. 
    countDownTimer = new CountDownTimer(1500, 1500) { 
     public void onTick(long millisUntilFinished) { 

     } 

     public void onFinish() { 
      // Here do what you like... 
      Intent intent = new Intent(context, Scores.class); 
      startActivity(intent); 
     } 
    }.start(); 
} 
+0

我怎樣才能讓這個是他們按下正確的按鈕計時器被取消,然後代碼開始再次爲下一個級別。 P.S我知道你是那個回答它的人,所以我會盡快給你代理 – user3200546

+0

我明白了,謝謝! – user3200546

+0

感謝隊友,很高興知道你解決了它。 –

0

你與CountDownTimer試過嗎?

下面是一個例子:

new CountDownTimer(1500, 1500) { 
     public void onTick(long millisUntilFinished) { 

     } 

     public void onFinish() { 
      // Here do what you like... 
     } 
    }.start(); 
+0

當使用onFinish \t 公共無效onFinish(){ \t \t \t意圖意圖=新意圖(此,Scores.class); \t \t \t startActivity(intent); \t} 它拋出了以下錯誤: 構造函數Intent(new CountDownTimer(){},類)未定義 – user3200546

0

要修復中提到的錯誤:

MainActivity context; 

@Override protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    context = this; 

    new CountDownTimer(1500, 1500) { 
     public void onTick(long millisUntilFinished) { 

     } 

     public void onFinish() { 
      // Here do what you like... 
      Intent intent = new Intent(context, Score.class); 
      startActivity(intent); 
     } 
    }.start(); 
相關問題