1

我创建了一个带有计时器的游戏,我试图让当计时器结束时,玩家会看到警报弹出窗口或任何类型的弹出窗口,上面写着“关卡完成”,你的积分是 xxx 和下一个关卡的按钮。我尝试了一些东西,但时间结束了,但没有弹出窗口。任何想法?

时间类:工作正常。

公共课时间{

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

}
4

2 回答 2

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

}

我在这里发布了一个非常相似的答案,但是在这个问题中没有计时器,而是某种“游戏结束”事件。 如何对另一个类的返回值进行连续检查?

以及关于侦听器模式或观察者模式的 wiki 链接 http://en.wikipedia.org/wiki/Observer_pattern

罗尔夫

于 2012-10-20T09:35:13.293 回答
0

使用TimerTimerTask运行计划的操作。例子在这里这里

于 2012-10-20T09:39:42.757 回答