我正在尝试编写一个Minecraft克隆。 在目前的状态,我有一个小世界,我也可以走动,但现在我坚持使用滴答方法。我在while循环的main方法中调用了这个tick方法。
在主方法中循环:
boolean closeRequested = false;
while(!closeRequested){
mc.tick();
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
closeRequested = true;
}
if(Display.isCloseRequested()){
closeRequested = true;
}
勾选方法:
public void tick()
{
while (Keyboard.next()) {
if (Keyboard.getEventKeyState())
{
if (Keyboard.getEventKey() == 28) {
Minecraft.level.save();
}
if (Keyboard.getEventKey() == 2) {
this.paintTexture = 1;
}
if (Keyboard.getEventKey() == 3) {
this.paintTexture = 3;
}
if (Keyboard.getEventKey() == 4) {
this.paintTexture = 4;
}
if (Keyboard.getEventKey() == 5) {
this.paintTexture = 5;
}
if (Keyboard.getEventKey() == 7) {
this.paintTexture = 6;
}
}
}
Minecraft.level.tick();
Minecraft.particleEngine.tick();
Minecraft.player.tick(); //This is the movement
}
玩家勾选方法:
public void tick()
{
this.xo = this.x;
this.yo = this.y;
this.zo = this.z;
float xa = 0.0F;
float ya = 0.0F;
if (Keyboard.isKeyDown(19)) {
resetPos();
}
if ((Keyboard.isKeyDown(200)) || (Keyboard.isKeyDown(17))) {
ya -= 1.0F;
}
if ((Keyboard.isKeyDown(208)) || (Keyboard.isKeyDown(31))) {
ya += 1.0F;
}
if ((Keyboard.isKeyDown(203)) || (Keyboard.isKeyDown(30))) {
xa -= 1.0F;
}
if ((Keyboard.isKeyDown(205)) || (Keyboard.isKeyDown(32))) {
xa += 1.0F;
}
if ((Keyboard.isKeyDown(57)) || (Keyboard.isKeyDown(219))) {
if (this.onGround) {
this.yd = 0.5F;
}
}
moveRelative(xa, ya, this.onGround ? 0.1F : 0.02F);
this.yd = ((float)(this.yd - 0.08D));
move(this.xd, this.yd, this.zd);
this.xd *= 0.91F;
this.yd *= 0.98F;
this.zd *= 0.91F;
if (this.onGround)
{
this.xd *= 0.7F;
this.zd *= 0.7F;
}
}
当我这样称呼这种方法时,玩家会像声音那样移动,而且取决于fps,玩家的移动速度也是如此。
我从Minecraft alpha版本获得了一些代码,但我找不到调用tick()
方法的点。
我希望玩家以恒定速度移动。
我还有一个计时器,我可以获得一些变量,如timepasssed,fps或ticks(计时器也来自原始的minecraft代码)。
答案 0 :(得分:1)
将此添加到您的代码中:
while(!closeRequested){
mc.tick();
//do stuff here that runs every 20 times in a second
Thread.sleep(50); //Sleep for 50 milliseconds, which is default minecraft gametick
}
没有sleep(),循环将尽可能快地运行,从而导致你的问题。一个简单的方法sleep()将使它休息片刻,然后再次使用循环。