2012-10-20 7 views
1

타이머가있는 게임을 만들었습니다. 타이머가 끝나면 플레이어가 경고 팝업이나 "레벨 완성"이라고하는 팝업을 보게됩니다. 점수는 xxx입니다. 다음 단계를위한 버튼. 뭔가를 시도했지만 시간이 지났지 만 팝업이 없습니다. 아이디어가 있으십니까?안드로이드 - 타이머 기능 및 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(); 

이는 활동 클래스입니다 :

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(); 
} 

더 좋은 옵션은 어떤 종류의 루프를 만드는 것이지만, 이것은 forbidden입니다! 주 스레드를 차단할 것이기 때문입니다.

다음 옵션은 어떤 종류의 리스너를 만드는 것입니다. 청취자는 당신의 활동에 콜백을하여 "끝났습니다"라고 말할 것입니다. 이것은 종종

작은 예, 인터페이스를 사용하여 수행됩니다 :

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?

그리고

+0

감사합니다, Excelent 솔루션, 경고에 버튼을 추가하는 것에 대한 조언이 있습니까? – cfircoo

+0

여기를 보시면 http://developer.android.com/guide/topics/ui/dialogs.html setPositiveButton과 Negative가 해결책이 될 수 있습니다. –