我正在尝试构建一个计时器,但出了点问题,我无法找出问题所在。这就是我做这个问题的原因。无论如何,下面是我到目前为止正在执行此操作的代码:https://www.youtube.com/watch?v=VfSYH0imDQk&feature=youtu.be&hd=1 有4个变量:Second4(第4个数字)、Second3(第3个数字)、Minute2(第2个数字)和Minute1(第1个数字)
if (TimeUtils.nanoTime() - timePassed >1000000000){
if(timerSecond4==10){
timerSecond4=0;
if(timer%10==0 &&timerSecond3!=5)
timerSecond3++;
else if(timerSecond3==5){
timerSecond3=0;
timerSecond4=0;
timerMinute2++;
if(timerMinute2%10==0){
timerMinute1++;
timerMinute2=0;
}
}
timerMinutes1=checkNumberTimer(timerMinute1);
batch.draw(timerMinutes1,550,380);
timerMinutes2=checkNumberTimer(timerMinute2);
batch.draw(timerMinutes2,590,380);
timerSeconds3=checkNumberTimer(timerSecond3);
batch.draw(timerSeconds3,650,380);
timerSeconds4=checkNumberTimer(timerSecond4);
batch.draw(timerSeconds4,690,380);
}
else{
timerMinutes1=checkNumberTimer(timerMinute1);
batch.draw(timerMinutes1,550,380);
timerMinutes2=checkNumberTimer(timerMinute2);
batch.draw(timerMinutes2,590,380);
timerSeconds3=checkNumberTimer(timerSecond3);
batch.draw(timerSeconds3,650,380);
timerSeconds4=checkNumberTimer(timerSecond4);
batch.draw(timerSeconds4,690,380);
}
batch.draw(colons,627,387);
timer++;
timerSecond4++;
timePassed();
}
public Texture checkNumberTimer(int t){
Texture n=null;
switch(t){
case 0:n= zeroTimer;break;
case 1:n= oneTimer;break;
case 2:n= twoTimer;break;
case 3:n= threeTimer;break;
case 4:n= fourTimer;break;
case 5:n= fiveTimer;break;
case 6:n= sixTimer;break;
case 7:n= sevenTimer;break;
case 8:n= eightTimer;break;
case 9:n= nineTimer;break;
}
return n;
}
public void timePassed() {
timePassed=TimeUtils.nanoTime();
}
最佳答案
呸! 我敢打赌它在你嵌套的“if”语句中的某个地方弄乱了。 测量整个游戏时间(从计时器开始到当前时间),除以秒、十秒、分钟和十分钟,然后显示出来不是更容易吗?
有点像:
long startTime;
long timePassed;
//while starting your game:
startTime=TimeUtils.millis();
//the difference, in milliseconds, between
//the current time and midnight, January 1, 1970 UTC.
[...]
//and then in render loop:
timePassed=TimeUtils.millis()-startTime;
//above line will give you time from game start, in miliseconds
timerSeconds4=(timePassed/1000)%10; //seconds
timerSeconds3=(timePassed/10000)%10; //ten-seconds
timerMinutes2=timePassed/60000; //minutes
timerMinutes1=(timePassed/600000)%10; //ten-minutes
batch.draw(timerMinutes1texture...
etc...
我没有测试它,但我相信,它应该工作得很好:)
另外,它与滞后无关,如果有什么东西延迟了你的循环,它不会对你的计时器产生影响
关于java - Libgdx 无法正确批处理计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28116291/