1
votes

I'm trying to reset my Countdown Timer with new variables (a new time), but nothing I do seems to work. Anyone know how to do this?

private long cdTime = 30000;
private long percentageTime = 30;

private void createCDTimer(){
    mCountDownTimer = new CountDownTimer(cdTime, 1000) {
        public void onTick(long millisUntilFinished) {
            score = millisUntilFinished / 1000;
            percentage = percentage - (100 / percentageTime);

            cdTime = millisUntilFinished;
            percentageTime = millisUntilFinished / 1000;
        }

        public void onFinish() {
            score = 0;
            percentage = 0;
        }

    }.start();
}

this code is in another class/void {
    mCountDownTimer.cancel();
    cdTime = cdTime + 20000;
    percentageTime = percentageTime + 20;
    mCountDownTimer.start();
}

Thanks in advance.

EDIT:

Here's the rest of my code for those who need it:

public class GameView extends SurfaceView implements Runnable {
Context context;

private Thread gameThread = null;
private SurfaceHolder ourHolder;
private volatile boolean playing;
private boolean paused = true;
private Canvas canvas;
private Paint paint;
private long fps;
private long thisTimeFrame;
private int screenX;
private int screenY;
private int column1;
private int column2;
private int column3;
private float enemyMDrop;
private float enemyMY;
private float enemyLDrop;
private float enemyLY;
private float enemyRDrop;
private float enemyRY;
private Random randomGen = new Random();
private int loadBarAnim;
private float loadBarTimer;
private float gameTimer;
private long percentage = 100;
private CountDownTimer mCountDownTimer;
private long cdTime;
private long percentageTime;

private Compressor compressor;
private loadingBar loadBar;
private loadingBarAnimation loadBarAnimation;
private EnemyMid enemyM;
private EnemyLeft enemyL;
private EnemyLeft enemyR;

private float mTimer;
private float lTimer;
private float rTimer;
private float enemyMWait;
private float enemyLWait;
private float enemyRWait;
private float enemyMTimer;
private float enemyLTimer;
private float enemyRTimer;
private boolean enemyMGo;
private boolean enemyLGo;
private boolean enemyRGo;

private SoundPool soundPool;
private int deathSound = -1;
private int liveSound = -1;
private int shootSound = -1;
private int gameMusic = -1;

private long score;
String scoreText = "Score: ";

//String scoreView = String.format("%.02f", score);
private int lives = 3;

public GameView (Context context, int x, int y){
    super(context);

    this.context = context;

    ourHolder = getHolder();
    paint = new Paint();

    screenX = x;
    screenY = y;

    prepeareLevel();
}

private void createCDTimer(){
    mCountDownTimer = new CountDownTimer(cdTime, 1000) {
        public void onTick(long millisUntilFinished) {
            score = millisUntilFinished / 1000;
            percentage = percentage - (100 / percentageTime);

            cdTime = millisUntilFinished;
            percentageTime = millisUntilFinished / 1000;
        }

        public void onFinish() {
            score = 0;
            percentage = 0;
        }

    }.start();
}

private void prepeareLevel (){

    cdTime = 30000;
    percentageTime = 30;

    createCDTimer();

    loadBarAnim = 1;

    column1 = screenX / 3;
    column2 = (screenX / 3 ) * 2;
    column3 = (screenX / 3 ) * 3;

    compressor = new Compressor(context, screenX, screenY);
    loadBar = new loadingBar(context, screenX, screenY);
    loadBarAnimation = new loadingBarAnimation(context, screenX, screenY, 2);
    enemyM = new EnemyMid(context, screenX, screenY);
    enemyL = new EnemyLeft(context, screenX, screenY);
    enemyR = new EnemyLeft(context, screenX, screenY);


    mTimer = 0;
    lTimer = 0;
    rTimer = 0;

    enemyMTimer = 2;
    enemyLTimer = 0;
    enemyRTimer = 5;

    enemyMGo = false;
    enemyLGo = false;
    enemyRGo = false;

    enemyMDrop = 0;
    enemyLDrop = 0;
    enemyRDrop = 0;

    enemyMWait = randomGen.nextInt((5 - 1) + 1);
    enemyLWait = randomGen.nextInt((5 - 1) + 1);
    enemyRWait = randomGen.nextInt((5 - 1) + 1);
}

@Override
public void run(){
    while (playing){
        long startFrameTime = System.currentTimeMillis();

        if(!paused){
            update();
        }

        draw();

        thisTimeFrame = System.currentTimeMillis() - startFrameTime;

        if(thisTimeFrame >= 1){
            fps = 1000/thisTimeFrame;
        }
    }
}

private void update(){
    boolean finished = false;

    if (finished){

    }

}

private void draw(){
    if(ourHolder.getSurface().isValid()){
        canvas = ourHolder.lockCanvas();

        if(rTimer < 30){
            rTimer++;
        }
        else {
            rTimer = 0;

            if (enemyRTimer >= enemyRWait-1){
                enemyRGo = true;
                enemyRTimer = 0;
            }
            else if (!enemyRGo){
                enemyRTimer++;
            }
        }

        if(mTimer < 30){
            mTimer++;
        }
        else {
            mTimer = 0;
            if (enemyMTimer >= enemyMWait-1){
                enemyMGo = true;
                enemyMTimer = 0;
            }
            else if (!enemyMGo){
                enemyMTimer++;
            }
        }

        if(lTimer < 30){
            lTimer++;
        }
        else {

            if (enemyLTimer >= enemyLWait-1){
                enemyLGo = true;
                enemyLTimer = 0;
            }
            else if (!enemyLGo){
                enemyLTimer++;
            }
        }

        canvas.drawColor(Color.WHITE);

        paint.setColor(Color.BLACK);

        if (enemyMGo){
            enemyMDrop = enemyMDrop - 10;
            enemyMY = enemyM.getY() - enemyMDrop;
        }

        if (enemyMY > 1200) {
            enemyMDrop = 0;
            enemyMY = 0;
            enemyMGo = false;
            enemyMTimer = 0;
            enemyMWait = randomGen.nextInt((5 - 1) + 1);

            mCountDownTimer.cancel();
            cdTime = cdTime + 20000;
            percentageTime = percentageTime + 20;

            mCountDownTimer = new CountDownTimer(cdTime, 1000) {
                public void onTick(long millisUntilFinished) {
                    score = millisUntilFinished / 1000;
                    percentage = percentage - (100 / percentageTime);

                    cdTime = millisUntilFinished;
                    percentageTime = millisUntilFinished / 1000;
                }
                public void onFinish() {
                    score = 0;
                    percentage = 0;
                }
            };

            mCountDownTimer.start();
        }

        if (enemyLGo){
            enemyLDrop = enemyLDrop - 10;
            enemyLY = enemyL.getY() - enemyLDrop;
        }

        if (enemyLY > 1200) {
            enemyLDrop = 0;
            enemyLY = 0;
            enemyLGo = false;
            enemyLTimer = 0;
            enemyLWait = randomGen.nextInt((5 - 1) + 1);
        }

        if (enemyRGo){
            enemyRDrop = enemyRDrop - 10;
            enemyRY = enemyR.getY() - enemyRDrop;
        }

        if (enemyRY > 1200) {
            enemyRDrop = 0;
            enemyRY = 0;
            enemyRGo = false;
            enemyRTimer = 0;
            enemyRWait = randomGen.nextInt((5 - 1) + 1);
        }

        //Draw the invaders
        canvas.drawBitmap(enemyL.getBitmap(), (column1 - enemyL.getX()) - (column1 / 4), enemyLY, paint);

        canvas.drawBitmap(enemyM.getBitmap(), (column2 - enemyM.getX()) - (column1 / 4), enemyMY, paint);

        canvas.drawBitmap(enemyR.getBitmap(), (column3 - enemyL.getX()) - (column1 / 4), enemyRY, paint);

        //draw the compressor
        canvas.drawBitmap(compressor.getBitmap(), 0, 0, paint);

        //draw the animated loading bar
        if (loadBarTimer > 2){
            loadBarTimer = 0;
            if (loadBarAnim == 1){
                loadBarAnim = 2;
            }
            else if (loadBarAnim == 2){
                loadBarAnim = 3;
            }
            else if (loadBarAnim == 3){
                loadBarAnim = 4;
            }
            else {
                loadBarAnim = 1;
            }
        }
        else {
            loadBarTimer++;
        }

        if (loadBarAnim == 1) {
            canvas.drawBitmap(loadBarAnimation.getBitmap1(), 19, 1300, paint);
        }
        else if (loadBarAnim == 2){
            canvas.drawBitmap(loadBarAnimation.getBitmap2(), 19, 1300, paint);
        }
        else if (loadBarAnim == 3) {
            canvas.drawBitmap(loadBarAnimation.getBitmap3(), 19, 1300, paint);
        }
        else {
            canvas.drawBitmap(loadBarAnimation.getBitmap4(), 19, 1300, paint);
        }

        //draw the loading bar
        canvas.drawBitmap(loadBar.getBitmap(), 20, 1300, paint);

        //Draw the Score
        paint.setColor(Color.BLACK);
        paint.setTextSize(40);
        canvas.drawText("Time: " + score, 100, 1600, paint);

        //Draw the Percentage
        paint.setColor(Color.BLACK);
        paint.setTextSize(40);
        canvas.drawText("Percentage: " + percentage, 725, 1600, paint);

        ourHolder.unlockCanvasAndPost(canvas);
    }
}

//Paused or Stopped
public void pause(){
    playing = false;
    try{
        gameThread.join();
    }
    catch (InterruptedException e){
        Log.e("Error: ", "Error joining thread");
    }
}

//Resume
public  void resume(){
    playing = true;
    gameThread = new Thread(this);
    gameThread.start();
}

public boolean onTouchEvent(MotionEvent motionEvent){
    switch (motionEvent.getAction() & MotionEvent.ACTION_MASK){
        //player touches the screen
        case MotionEvent.ACTION_DOWN:
            paused = false;

            if (motionEvent.getX() > screenX - column1 && enemyRY > 200){
                if (motionEvent.getY() < screenY) {
                    enemyRWait = randomGen.nextInt((5 - 1) + 1);
                    enemyRY = 0;
                    enemyRDrop = 0;
                    enemyRGo = false;
                    enemyRTimer = 0;
                    rTimer = 0;
                    canvas = ourHolder.lockCanvas();

                    canvas.drawColor(Color.WHITE);

                    paint.setColor(Color.BLACK);

                    //Draw the invaders
                    canvas.drawBitmap(enemyL.getBitmap(), (column1 - enemyL.getX()) - (column1 / 4), enemyLY, paint);

                    canvas.drawBitmap(enemyM.getBitmap(), (column2 - enemyM.getX()) - (column1 / 4), enemyMY, paint);

                    canvas.drawBitmap(enemyR.getBitmap(), (column3 - enemyL.getX()) - (column1 / 4), enemyRY, paint);

                    //draw the compressor
                    canvas.drawBitmap(compressor.getBitmap(), 0, 0, paint);

                    //draw the animated loading bar
                    if (loadBarTimer > 2){
                        loadBarTimer = 0;
                        if (loadBarAnim == 1){
                            loadBarAnim = 2;
                        }
                        else if (loadBarAnim == 2){
                            loadBarAnim = 3;
                        }
                        else if (loadBarAnim == 3){
                            loadBarAnim = 4;
                        }
                        else {
                            loadBarAnim = 1;
                        }
                    }
                    else {
                        loadBarTimer++;
                    }

                    if (loadBarAnim == 1) {
                        canvas.drawBitmap(loadBarAnimation.getBitmap1(), 19, 1300, paint);
                    }
                    else if (loadBarAnim == 2){
                        canvas.drawBitmap(loadBarAnimation.getBitmap2(), 19, 1300, paint);
                    }
                    else if (loadBarAnim == 3) {
                        canvas.drawBitmap(loadBarAnimation.getBitmap3(), 19, 1300, paint);
                    }
                    else {
                        canvas.drawBitmap(loadBarAnimation.getBitmap4(), 19, 1300, paint);
                    }

                    //draw the loading bar
                    canvas.drawBitmap(loadBar.getBitmap(), 20, 1300, paint);

                    //Draw the Score
                    paint.setColor(Color.BLACK);
                    paint.setTextSize(40);
                    canvas.drawText("Time: " + score, 100, 1600, paint);

                    //Draw the Percentage
                    paint.setColor(Color.BLACK);
                    paint.setTextSize(40);
                    canvas.drawText("Percentage: " + percentage, 725, 1600, paint);

                    ourHolder.unlockCanvasAndPost(canvas);
                }
            }

            if (motionEvent.getX() > screenX - column2 && motionEvent.getX() < screenX - column1 && enemyMY > 200){
                if (motionEvent.getY() < screenY) {
                    enemyMWait = randomGen.nextInt((5 - 1) + 1);
                    enemyMY = 0;
                    enemyMDrop = 0;
                    enemyMGo = false;
                    enemyMTimer = 0;
                    mTimer = 0;
                    canvas = ourHolder.lockCanvas();

                    canvas.drawColor(Color.WHITE);

                    paint.setColor(Color.BLACK);

                    //Draw the invaders
                    canvas.drawBitmap(enemyL.getBitmap(), (column1 - enemyL.getX()) - (column1 / 4), enemyLY, paint);

                    canvas.drawBitmap(enemyM.getBitmap(), (column2 - enemyM.getX()) - (column1 / 4), enemyMY, paint);

                    canvas.drawBitmap(enemyR.getBitmap(), (column3 - enemyL.getX()) - (column1 / 4), enemyRY, paint);

                    //draw the compressor
                    canvas.drawBitmap(compressor.getBitmap(), 0, 0, paint);

                    //draw the animated loading bar
                    if (loadBarTimer > 2){
                        loadBarTimer = 0;
                        if (loadBarAnim == 1){
                            loadBarAnim = 2;
                        }
                        else if (loadBarAnim == 2){
                            loadBarAnim = 3;
                        }
                        else if (loadBarAnim == 3){
                            loadBarAnim = 4;
                        }
                        else {
                            loadBarAnim = 1;
                        }
                    }
                    else {
                        loadBarTimer++;
                    }

                    if (loadBarAnim == 1) {
                        canvas.drawBitmap(loadBarAnimation.getBitmap1(), 19, 1300, paint);
                    }
                    else if (loadBarAnim == 2){
                        canvas.drawBitmap(loadBarAnimation.getBitmap2(), 19, 1300, paint);
                    }
                    else if (loadBarAnim == 3) {
                        canvas.drawBitmap(loadBarAnimation.getBitmap3(), 19, 1300, paint);
                    }
                    else {
                        canvas.drawBitmap(loadBarAnimation.getBitmap4(), 19, 1300, paint);
                    }

                    //draw the loading bar
                    canvas.drawBitmap(loadBar.getBitmap(), 20, 1300, paint);

                    //Draw the Score
                    paint.setColor(Color.BLACK);
                    paint.setTextSize(40);
                    canvas.drawText("Time: " + score, 100, 1600, paint);

                    //Draw the Percentage
                    paint.setColor(Color.BLACK);
                    paint.setTextSize(40);
                    canvas.drawText("Percentage: " + percentage, 725, 1600, paint);

                    ourHolder.unlockCanvasAndPost(canvas);
                }
            }

            if (motionEvent.getX() > screenX - column3 && motionEvent.getX() < screenX - column2 && enemyLY > 200){
                if (motionEvent.getY() < screenY) {
                    enemyLWait = randomGen.nextInt((5 - 1) + 1);
                    enemyLY = 0;
                    enemyLDrop = 0;
                    enemyLGo = false;
                    enemyLTimer = 0;
                    lTimer = 0;
                    canvas = ourHolder.lockCanvas();

                    canvas.drawColor(Color.WHITE);

                    paint.setColor(Color.BLACK);

                    //Draw the invaders
                    canvas.drawBitmap(enemyL.getBitmap(), (column1 - enemyL.getX()) - (column1 / 4), enemyLY, paint);

                    canvas.drawBitmap(enemyM.getBitmap(), (column2 - enemyM.getX()) - (column1 / 4), enemyMY, paint);

                    canvas.drawBitmap(enemyR.getBitmap(), (column3 - enemyL.getX()) - (column1 / 4), enemyRY, paint);

                    //draw the compressor
                    canvas.drawBitmap(compressor.getBitmap(), 0, 0, paint);

                    //draw the animated loading bar
                    if (loadBarTimer > 2){
                        loadBarTimer = 0;
                        if (loadBarAnim == 1){
                            loadBarAnim = 2;
                        }
                        else if (loadBarAnim == 2){
                            loadBarAnim = 3;
                        }
                        else if (loadBarAnim == 3){
                            loadBarAnim = 4;
                        }
                        else {
                            loadBarAnim = 1;
                        }
                    }
                    else {
                        loadBarTimer++;
                    }

                    if (loadBarAnim == 1) {
                        canvas.drawBitmap(loadBarAnimation.getBitmap1(), 19, 1300, paint);
                    }
                    else if (loadBarAnim == 2){
                        canvas.drawBitmap(loadBarAnimation.getBitmap2(), 19, 1300, paint);
                    }
                    else if (loadBarAnim == 3) {
                        canvas.drawBitmap(loadBarAnimation.getBitmap3(), 19, 1300, paint);
                    }
                    else {
                        canvas.drawBitmap(loadBarAnimation.getBitmap4(), 19, 1300, paint);
                    }

                    //draw the loading bar
                    canvas.drawBitmap(loadBar.getBitmap(), 20, 1300, paint);

                    //Draw the Score
                    paint.setColor(Color.BLACK);
                    paint.setTextSize(40);
                    canvas.drawText("Time: " + score, 100, 1600, paint);

                    //Draw the Percentage
                    paint.setColor(Color.BLACK);
                    paint.setTextSize(40);
                    canvas.drawText("Percentage: " + percentage, 725, 1600, paint);

                    ourHolder.unlockCanvasAndPost(canvas);
                }
            }

            break;

    }

    return true;
}
}

Error codes:

11-22 06:41:46.164 9091-9146/au.com.webanddesignbros.loadingbarsimulator W/dalvikvm: threadid=12: thread exiting with uncaught exception (group=0xa65f7228)
11-22 06:41:46.174 9091-9146/au.com.webanddesignbros.loadingbarsimulator E/AndroidRuntime: FATAL EXCEPTION: Thread-206
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.(Handler.java:121)
at android.os.CountDownTimer$1.(CountDownTimer.java:109)
at android.os.CountDownTimer.(CountDownTimer.java:109)
at au.com.webanddesignbros.loadingbarsimulator.GameView$2.(GameView.java:0)
at au.com.webanddesignbros.loadingbarsimulator.GameView.draw(GameView.java:250)
at au.com.webanddesignbros.loadingbarsimulator.GameView.run(GameView.java:164)
at java.lang.Thread.run(Thread.java:856)

1

1 Answers

0
votes

You are canceling the countdown and starting it again without every setting the new time values. You are in fact only changing your cdTime and percentageTime variables. You should create a new countdown timer with these updated values.

mCountDownTimer.cancel();
cdTime = cdTime + 20000;
percentageTime = percentageTime + 20;
//Create a countdown timer with the updated time
mCountDownTimer = new CounDownTimer(cdTime, anInterval){ 
                     /* handle the countdown notifications as you wish, for example like you did in your question */ 
                   }
mCountDownTimer.start();

In order to validate that the code above should work a test app has been made. The code and apk are available at this repository : https://github.com/ySiggen/CDTimerTest

After the stack trace has been added to the question, this answer has been edited with the following:

The error you get means that you are calling code (the count down timer) from a background thread while it should be done from the main thread.

You can use a Handler to call the code from another thread like this:

new Handler(Looper.getMainLooper()).post(new Runnable() {           
    @Override
    public void run() {
        mCountDownTimer  = new CountDownTimer(cdTime, 1000) {
        /* handle the countdown notifications as you wish, for example like you did in your question */ 
        }.start();
    } 
});