halo_4
halo_4

Reputation: 63

Android countdown timer for a game

ok heres the problem im have i have made a fully working game, with menu and help page score lives levels, but i can't get the timer working as it should, at 1st i thought it was an if() but i ran it it would go down to fast.

i have made the game in using android java (eclipse), i would post some code up but im not sure which bits you would like to see, i now that secs in java are milisecs so 50000/1000 = 50secs but i dont know how to do it, i looked a few exmaple code and im not sure how it is working.

the timer is a label i made that appear on the AVD, atm it just says 50000 and not doing anything

i tried doing it this way

public class timer extends CountDownTimer{

        public timer(long timer, long Interval) {

            super(timer, Interval);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            //canvas.drawText("GAME OVER", 200, 700, paint);

        }

        @Override
        public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub

            realtime = (int) millisUntilFinished;

        }

    }

Upvotes: 0

Views: 4630

Answers (1)

Justin Mclean
Justin Mclean

Reputation: 1615

When the game starts save the system time and create a new Paint instance with the text size and color you want.

    startTime = System.currentTimeMillis();
    text = new Paint();
    text.setColor(Color.RED);
    text.setTextSize(20);

To draw the text onto the canvas / screen inside your onDrow method do something like this.

    timeNow = System.currentTimeMillis();
    long timeToGo = 50 - (timeNow - startTime) / 1000;
    if (timeToGo >= 0) {
        canvas.drawText(Long.toString(timeToGo), 10, 25, text);
    }

This will place a red count down timer in the top left of your screen that counts down from 50.

Upvotes: 2

Related Questions