Zubaja
Zubaja

Reputation: 261

Why is my CountDownTimer counting faster than given parameters?

I'm having an issue with my CountDownTimer. Here's the situation: Working on a game and I am not using a framework (who needs one, right? coughs) and I have a CountDownTimer running. This CountDownTimer is used to calculate how long a player has to complete a level, and also update the screen per 100ms tick.

In the first level this works fine, but in the second level it ticks per 50ms, and in the third per 60/90/50ms ticks (yes, in that order repeatedly) making the game behave odly and messing up collision. I have no idea what's going on here.

I've ruled out if it's dependant on the level by loading later levels first, and upon solving those the problem arrises all the same. I have, however, pinpointed where it's going wrong: During the loading for a new level, and only then; even reloading a current level doesn't bring this bug up.

I have a suspicion that somehwere in this code, something's not going as it should be:

public void prepareNewLevel(){  
    RelativeLayout rl = (RelativeLayout)findViewById(R.id.game);

    level.clear();
    movables.clear();
    img_move.clear();
    img_noMove.clear();
    timerCount.cancel();
    totalSolved = 0;
    solvable = 0;
    levelWidth = 0;
    levelHeight = 0;
    allCollided = false;
    firstDraw = true;

    rl.removeAllViewsInLayout();
}

level, movables, img_move and img_noMove are List items.

This my timer:

MyCount update = new MyCount(15*1000, 100);

This is MyCount:

public class MyCount extends CountDownTimer{

    public MyCount(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);

    }

    @Override
    public void onTick(long millisUntilFinished) {
        remaining_time = millisUntilFinished;

        if(!loadingNewLevel)
            MoveObjects();
    }

    @Override
    public void onFinish() {
        resetLevel = true;
        upMotion = false;
        downMotion = false;
        leftMotion = false;
        rightMotion = false;
        update.start();
    }
}

And then I also call update.start() at the end of my loadLevel() function. I first thought I had two timers running at once, but writing to the LogCat showed me that was false, concidering I got a neat 'reset' message every 15 seconds, instead of at multiple moments smaller than 15s.

And that's basically the issue here. Now that I know where it's being caused and what the issue is, how do I solve it?

-Zubaja

Upvotes: 0

Views: 619

Answers (2)

Tapa Save
Tapa Save

Reputation: 4857

Try use Timer instead CountDownTimer:

  // start timer inside your method
  if( timer != null)  timer.cancel();
  timer = new Timer();
  timer.scheduleAtFixedRate( new TimerTask()
   {
     @Override
     public void run()
      {
        handler.obtainMessage( yourInegerMessage).sendToTarget();
      }
   }, 100, 15000);      

  // and define you handler
  public Handler handler = new Handler( new Handler.Callback()
   {
     @Override
     public boolean handleMessage( Message message)
      {
        // use 'message.what' as yourInegerMessage
        // ...
        return true;
      }
   });

  // and stop your timer after first time

May be this help you.

Upvotes: 1

Tapa Save
Tapa Save

Reputation: 4857

May be you start more one CountDownTimer? Try stop existing CountDownTimer before start new CountDownTimer.

Upvotes: 0

Related Questions