简体   繁体   English

如何调用tick()方法

[英]How to call the tick() method

I am trying to program a minecraft clone.我正在尝试编写一个 minecraft 克隆程序。 In the current state i have a little world and i can also walk around but now i am stuck with the tick method.在目前的状态下,我有一个小世界,我也可以四处走动,但现在我坚持使用滴答方法。 I'm calling this tick method in the main method in the while loop.我在 while 循环的 main 方法中调用了这个 tick 方法。

While loop in the main method:主方法中的while循环:

boolean closeRequested = false;
while(!closeRequested){

    mc.tick();

    if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
        closeRequested = true;
    }

    if(Display.isCloseRequested()){
        closeRequested = true;
    }

Tick Method:勾选方法:

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
  }

Player Tick Method:玩家勾选方法:

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;
}

} }

When i call this method like this the player moves like sonic and also its depending on the fps how fast the player moves.当我像这样调用这个方法时,玩家会像声波一样移动,并且它取决于玩家移动的速度。

I've got some of this code from a minecraft alpha version but i couldn't find the point where the tick() method is called.我从我的世界 alpha 版本中得到了一些代码,但我找不到调用tick()方法的点。

I want the player to move in a constant speed.我希望玩家以恒定的速度移动。

I also have a timer where i can get some variables like timepassed, fps or also the ticks (the timer is also from the original minecraft code).我还有一个计时器,我可以在其中获取一些变量,例如 timepassed、fps 或刻度(计时器也来自原始的 minecraft 代码)。

Add this to your code:将此添加到您的代码中:

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
}

Without the sleep() the loop will run as fast as it can, causing the problem of yours.如果没有 sleep(),循环将尽可能快地运行,从而导致您的问题。 A simple way sleep() will make it rest for a moment before countiuing the loop again.一个简单的方法 sleep() 将使它在再次计算循环之前休息片刻。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM