I created a game with timer, I am trying to make that when the timer is end the player will see alert popup or any kind of pop up that say “level complete”, your points are xxx and a button for next level. I tried something but the time over but no popup.
any idea?
Time Class: work fine.
public class Time {
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();
This is the Activity class:
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();
}
}
The point is that the timer takes some while to run out, and you are checking just once if the timer is done:
A better options would be to create some kind of loop, but this is forbidden! Because you would block the main thread.
The next option you have is to create some kind of listener. The listener will do some kind of callback to your activity to tell “i’m done”. This is often done using interfaces,
Small Example:
And your activity would look like this:
I have posted a very similar answer here, but in this question there is no timer but some kind of a “gameover” event.
How do I perform a continuous check on andorid of the returned value of another class?
And a wiki link about the listener pattern, or observer pattern
http://en.wikipedia.org/wiki/Observer_pattern
Rolf