我在 J2ME 中工作,我的游戏循环执行以下操作:
public void run() {
Graphics g = this.getGraphics();
while (running) {
long diff = System.currentTimeMillis() - lastLoop;
lastLoop = System.currentTimeMillis();
input();
this.level.doLogic();
render(g, diff);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
stop(e);
}
}
}
所以它只是一个基本的游戏循环,该doLogic()
函数调用场景中角色的所有逻辑函数并render(g, diff)
调用场景animateChar
中每个角色的animChar
函数,然后,Character类中的函数将屏幕中的所有内容设置为:
protected void animChar(long diff) {
this.checkGravity();
this.move((int) ((diff * this.dx) / 1000), (int) ((diff * this.dy) / 1000));
if (this.acumFrame > this.framerate) {
this.nextFrame();
this.acumFrame = 0;
} else {
this.acumFrame += diff;
}
}
这确保了我必须根据机器从一个周期到另一个周期所花费的时间来移动所有东西(记住它是一部手机,而不是游戏装备)。我确信这不是实现这种行为的最有效方法,所以我完全愿意在评论中批评我的编程技巧,但我的问题是:当我让我的角色跳跃时,我所做的就是把他的dy为负值,例如 -200,我将布尔跳转设置为 true,这使角色上升,然后我调用了这个函数checkGravity()
,以确保上升的所有内容都必须下降,checkGravity
同时检查角色是否在平台上,所以为了您的时间,我将把它删掉一点:
public void checkGravity() {
if (this.jumping) {
this.jumpSpeed += 10;
if (this.jumpSpeed > 0) {
this.jumping = false;
this.falling = true;
}
this.dy = this.jumpSpeed;
}
if (this.falling) {
this.jumpSpeed += 10;
if (this.jumpSpeed > 200) this.jumpSpeed = 200;
this.dy = this.jumpSpeed;
if (this.collidesWithPlatform()) {
this.falling = false;
this.standing = true;
this.jumping = false;
this.jumpSpeed = 0;
this.dy = this.jumpSpeed;
}
}
}
所以,问题是,这个函数会更新dy而不管diff,使角色在慢速机器中像超人一样飞行,我不知道如何实现diff因子,这样当一个角色跳跃时,他的速度减慢与游戏速度成正比的方式。谁能帮我解决这个问题?或者给我指点如何在 J2ME 中以正确的方式进行 2D 跳转。