2012-10-20 119 views
1

我創建了一個帶有計時器的遊戲,我試圖讓計時器結束時玩家會看到提醒彈出窗口或任何類型的彈出說「級別完成」,你的點是xxx和下一級的按鈕。我試了一些,但時間過去了,但沒有彈出。 有什麼想法?Android - 計時器功能和alertDialog

時間等級:正常工作。

公共類時間{

private String time; 
private boolean isDone; 

public Time() { 
    super(); 
    isDone=false; 
} 

CountDownTimer count = new CountDownTimer(5000, 1000) { 

public void onTick(long millisUntilFinished) { 

    int seconds = (int) (millisUntilFinished/1000); 
    int minutes = seconds/60; 
    seconds = seconds % 60; 
    String tempSec=Integer.toString(seconds); 
    if (tempSec.length()==1){ 
     tempSec="0"+tempSec; 
    } 
    time="Time Left: " + minutes + ":"+tempSec; 
} 

public void onFinish() { 
    setDone(true); 
} 

}.start(); 

這是Activity類:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 

    club=new Club(); 
    clubView = new ClubView(this, club); 
    mole=new Mole(); 
    stageView=new StageView(this); 
    moleView=new MoleView(this,mole); 
    pointsView=new PointsView(this); 

    time=new Time(); 
    timerView=new TimerView(this, time); 

    allViews=new AllViews(this); 
    allViews.setViews(stageView, moleView, pointsView, timerView,clubView); 

    setContentView(allViews); 
    allViews.setOnTouchListener((View.OnTouchListener)this); 

    if (timerView.getTime().isDone()){ 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage("Level Complete"); 
     builder.setMessage("your score is"+pointsView.getPoint()); 
     AlertDialog dialog = builder.create(); 
     dialog.show(); 
    } 

} 

回答

1

的一點是,計時器需要一些時間才能跑出來,你正在檢查只是一次如果定時器完成:

if (timerView.getTime().isDone()){ 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setMessage("Level Complete"); 
    builder.setMessage("your score is"+pointsView.getPoint()); 
    AlertDialog dialog = builder.create(); 
    dialog.show(); 
} 

更好的選擇是創建某種循環,但這是禁止!因爲你會阻止主線程。

您擁有的下一個選項是創建某種監聽器。聽衆會對你的活動進行一些回調,告訴「我完成了」。這是使用接口經常做,

小例子:

public class Time { 

    private String time; 
    private boolean isDone; 
    private TimerCallback timerCallback; 

    public Time(TimerCallback t) { 
     this.timerCallback = t; 
     isDone = false; 
    } 

    public interface TimerCallback { 
     abstract void onTimerDone(); 
    } 

    CountDownTimer count = new CountDownTimer(5000, 1000) { 

     public void onTick(long millisUntilFinished) { 

      int seconds = (int) (millisUntilFinished/1000); 
      int minutes = seconds/60; 
      seconds = seconds % 60; 
      String tempSec = Integer.toString(seconds); 
      if (tempSec.length() == 1) { 
       tempSec = "0" + tempSec; 
      } 
      time = "Time Left: " + minutes + ":" + tempSec; 
     } 

     public void onFinish() { 
      setDone(true); 
      timerCallback.onTimerDone(); 
     } 

    }.start(); 

} 

而且你的活動應該是這樣的:

public class myActivity extends Activity implements TimerCallback { 
//I have now clue how your activity is named but it's just an example! 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 

     club = new Club(); 
     clubView = new ClubView(this, club); 
     mole = new Mole(); 
     stageView = new StageView(this); 
     moleView = new MoleView(this, mole); 
     pointsView = new PointsView(this); 

     //Because we are implementing the TimerCallback interface "this" is a valid argument 
     //this can be cast to TimerCallback: "(TimerCallback) this" 
     time = new Time(this); 
     timerView = new TimerView(this, time); 

     allViews = new AllViews(this); 
     allViews.setViews(stageView, moleView, pointsView, timerView, clubView); 

     setContentView(allViews); 
     allViews.setOnTouchListener((View.OnTouchListener) this); 


    } 

    //We must add this method, because we have implemented the TimerCallback interface! 
    public void onTimerDone(){ 
     //You could remove the isDone check because it is not really necessary 
     if (timerView.getTime().isDone()) { 
      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setMessage("Level Complete"); 
      builder.setMessage("your score is" + pointsView.getPoint()); 
      AlertDialog dialog = builder.create(); 
      dialog.show(); 
     } 
    } 

} 

我已經張貼在這裏一個非常類似的答案,但在這個問題上有不是計時器,而是某種「遊戲轉移」事件。 How do I perform a continuous check on andorid of the returned value of another class?

約在監聽模式,或觀察者模式 http://en.wikipedia.org/wiki/Observer_pattern

羅爾夫

+0

謝謝,優秀的解決方案,任何建議添加按鈕的警報? – cfircoo

+0

看看這裏,http://developer.android.com/guide/topics/ui/dialogs.html setPositiveButton和Negative可能是一個解決方案。 –