简体   繁体   English

如何在Android中使用计时器进行评分?

[英]How to use timer for scoring in Android?

I'm planning to use a visible timer that will count down from 30 seconds to 0 seconds. 我打算使用一个可见的计时器,该计时器将从30秒倒计时到0秒。 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). 此计时器将成为我的游戏应用程序评分的基础,因为每次触摸移动的精灵(包含数字)但与请求的数字不匹配(如窗口/通知中所示)时,都会扣除当前剩余时间5秒(-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: 下面是该代码touchEvent里面GameView类:

@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). 每次触摸移动的精灵周围的区域时( sprite.wasPopped ),都会将其从屏幕上移除(并从分配的列表中移除),并spritePopped显示图像( spritePopped )以指示已触摸的位置(以增强效果)。 。 How do I create a separate class for the timer and utilize it in my GameView ? 如何为计时器创建单独的类并在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. 到目前为止,我遇到了使用滴答声进行评分的方法,该评分是根据某个类中调用了多少update()事件来计时的。 Any comments/suggestions would be very helpful. 任何意见/建议都将非常有帮助。

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. 您可以使用TimerTask但不建议这样做,因为由于未在主线程上运行,它可能会产生一些不良影响。

What you should do is get the deltaTime and store that in a variable. 您应该做的是获取deltaTime并将其存储在变量中。

    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. 您不应在更新方法中使用任何类型的计数器来检查时间或增加延迟,因为这会对无法以完整FPS运行游戏的设备产生不良影响。

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): 然后在GameView类的一个单独函数中调用此函数(游戏开始后即开始):

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM