我正在尝试构建一个计时器,但出现了问题,我无法找到答案。这就是为什么我在做这个问题。无论如何,下面是我到目前为止执行此操作的代码: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();
}
答案 0 :(得分:1)
呸! 我打赌它在你的嵌套'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...
我没有测试它,但我相信,它应该可以正常工作:)
另外,它与延迟无关,如果某些事情延迟了你的循环,它将对你的计时器没有影响