Question

I'm planning to use a visible timer that will count down from 30 seconds to 0 seconds. This timer will be the basis for scoring in my game application since each time a moving sprite (containing a number) is touched but does not match the number being requested (as shown in a window/notice), the current time remaining will be deducted by 5 seconds (-5). But if the sprite containing the matching number is touched, the timer stops and the game restarts (or moves to the next level).

Here's the code for the touchEvent inside the GameView class:

@Override
   public boolean onTouchEvent(MotionEvent event) {
         if (System.currentTimeMillis() - lastClick > 300) {
                lastClick = System.currentTimeMillis();
                float x = event.getX();
                float y = event.getY();
                synchronized (getHolder()) {
                       for (int i = sprites.size() - 1; i >= 0; i--) {
                              Sprite sprite = sprites.get(i);
                              if (sprite.wasPopped(x, y)) {
                                    sprites.remove(sprite);
                                    spritePopped.add(new TempSprite(temps, this, x, y, pop));
                                    break;
                              }
                       }
                }
         }
         return true;
   }

Every time an area surrounding a moving sprite is touched (sprite.wasPopped), it is removed from the screen (and from the list it was assigned) and an image (spritePopped) follows to indicate where it has been touched (for added effect). How do I create a separate class for the timer and utilize it in my GameView? So far, I've come across using ticks for scoring which is timed according to how many update() events were called in a certain class. Any comments/suggestions would be very helpful.

Was it helpful?

Solution

You could use a TimerTask but thats not to recommended since it can give some unwanted effects due to not running on your main-thread.

What you should do is get the deltaTime and store that in a variable.

    private float displayedTime;

    Public void timer(){ //call in update()
      displayedTime += deltaTime;
  }

Then you can mix around with this to get full seconds or whatever timer you prefer.

You should not use any sort of counter in your update method to check time or add delay since it will get a bad effect on devices that cant run your game at full FPS.

OTHER TIPS

I've found a similar question regarding this, here the the timer gets created in a separate class:

import android.os.CountDownTimer;
import android.widget.TextView;

public class MyCount extends CountDownTimer {
static TextView timeDisplay;

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

public void onFinish() {
    timeDisplay.setText("Time has ended. Game over.");
    //restart(); 
}

public void onTick(long millisUntilFinished) {
    timeDisplay.setText("Left: " + millisUntilFinished / 1000);
}

}

this is then called in a separate function in the GameView class (which starts once the the game begins):

public void setTimer() { 
timeDisplay = new TextView(this);
this.setContentView(timeDisplay);
MyCount counter = new MyCount(30000, 1000);
counter.start();

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top