如何在Java';s第三方API“;LibGDX";?
我正在做一个游戏,玩家(“鲍勃”)垂直移动,不断收集硬币。如果玩家在5秒内没有收集到任何硬币,“Bob”开始掉落。随着时间继续倒数,他会跌得更快 我的问题是:如何跟踪LibGDX(Java)应用程序中经过的时间? 下面是示例代码如何在Java';s第三方API“;LibGDX";?,java,libgdx,Java,Libgdx,我正在做一个游戏,玩家(“鲍勃”)垂直移动,不断收集硬币。如果玩家在5秒内没有收集到任何硬币,“Bob”开始掉落。随着时间继续倒数,他会跌得更快 我的问题是:如何跟踪LibGDX(Java)应用程序中经过的时间? 下面是示例代码 public void update (float deltaTime) { `velocity.add(accel.x * deltaTime,accel.y*deltaTime);` position.add(velocity.x * deltaTime,
public void update (float deltaTime)
{
`velocity.add(accel.x * deltaTime,accel.y*deltaTime);`
position.add(velocity.x * deltaTime, velocity.y * deltaTime);
bounds.x = position.x - bounds.width / 2;
bounds.y = position.y - bounds.height / 2;
if (velocity.y > 0 && state == BOB_COLLECT_COINE)
{
if (state== BOB_STATE_JUMP)
{
state = BOB_STATE_Increase;
stateTime = 0;
}
else
{
if(state != BOB_STATE_JUMP)
{
state = BOB_STATE_JUMP;//BOB_STATE_JUMP
stateTime = 0;
}
}
}
if (velocity.y < 0 && state != BOB_COLLECT_COINE)
{
if (state != BOB_STATE_FALL) {
state = BOB_STATE_FALL;
stateTime = 0;
}
}
if (position.x < 0) position.x = World.WORLD_WIDTH;
if (position.x > World.WORLD_WIDTH) position.x = 0;
stateTime += deltaTime;
}
public void hitSquirrel ()
{
velocity.set(0, 0);
state = BOB_COLLECT_COINE;s
stateTime = 0;
}
public void collectCoine()
{
state = BOB_COLLECT_COINE;
velocity.y = BOB_JUMP_VELOCITY *1.5f;
stateTime = 0;
}
public void更新(float deltaTime)
{
`速度加(加速度x*增量,加速度y*增量)`
position.add(velocity.x*deltaTime,velocity.y*deltaTime);
bounds.x=position.x-bounds.width/2;
bounds.y=position.y-bounds.height/2;
如果(velocity.y>0&&state==BOB\u COLLECT\u COINE)
{
if(state==BOB\u state\u JUMP)
{
状态=BOB_状态_增加;
stateTime=0;
}
其他的
{
if(state!=BOB\u state\u JUMP)
{
state=BOB\u state\u JUMP;//BOB\u state\u JUMP
stateTime=0;
}
}
}
if(velocity.y<0&&state!=BOB\u COLLECT\u COINE)
{
如果(状态!=BOB_状态_FALL){
状态=BOB_state_FALL;
stateTime=0;
}
}
如果(position.x<0)position.x=World.World\u WIDTH;
如果(position.x>World.World\u WIDTH)position.x=0;
stateTime+=deltaTime;
}
公共资源管理器()
{
速度设置(0,0);
state=BOB\u COLLECT\u COINE;s
stateTime=0;
}
公共无效集合币()
{
状态=BOB_COLLECT_COINE;
速度.y=BOB_跳跃速度*1.5f;
stateTime=0;
}
并在upate Bob中调用世界级的collectmethod作为--
private void updateBob(float deltaTime、float accelX)
{
diff=碰撞时间系统.currentTimeMillis();
如果(bob.state!=bob.bob\u COLLECT\u COINE&&diff>2000)//bob.position.y您是否尝试过使用Gdx.graphics.getElapsedTime()
(不确定确切的函数名)
build 0.9.7中的方法是“Gdx.graphics.getDeltaTime()”,因此上面的建议非常恰当。我就是这样做的
float time=0;
public void update(deltaTime){
time += deltaTime;
if(time >= 5){
//Do whatever u want to do after 5 seconds
time = 0; //i reset the time to 0
}
}
它是
鉴于这个答案有很多不同的观点,我应该用公认的答案指出一个问题,并提供另一个解决方案
由于以下代码行引起的舍入,程序运行时间越长,“计时器”将缓慢漂移:
time = 0;
原因是if条件检查时间值是否大于或等于5(很可能由于舍入误差而更大,帧间时间可能会变化)。更可靠的解决方案是不“重置”时间,而是减去等待的时间:
private static final float WAIT_TIME = 5f;
float time = 0;
public void update(float deltaTime) {
time += deltaTime;
if (time >= WAIT_TIME) {
// TODO: Perform your action here
// Reset timer (not set to 0)
time -= WAIT_TIME;
}
}
在快速测试过程中,您很可能不会注意到这个微妙的问题,但是如果您仔细查看事件的时间安排,在运行应用程序几分钟后,您可能会开始注意到它。正如答案所说,使用Gdx.graphics.getDeltaTime();
。
time = 0;
private static final float WAIT_TIME = 5f;
float time = 0;
public void update(float deltaTime) {
time += deltaTime;
if (time >= WAIT_TIME) {
// TODO: Perform your action here
// Reset timer (not set to 0)
time -= WAIT_TIME;
}
}