我正在尝试编写一个《我的世界》克隆程序。 在目前的状态下,我有一个小世界,我也可以四处走动,但现在我被蜱虫方法困住了。我在 while 循环的 main 方法中调用这个 tick 方法。
主方法中的While循环:
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()
方法的点。
我希望玩家以恒定速度移动。
我还有一个计时器,我可以在其中获取一些变量,例如耗时、fps 或刻度(计时器也来自原始的 Minecraft 代码)。
最佳答案
将其添加到您的代码中:
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() 会在再次计数循环之前让它休息一会儿。
关于java - 如何调用tick()方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41872892/