[英]How to call the tick() method
我正在嘗試編寫一個 minecraft 克隆程序。 在目前的狀態下,我有一個小世界,我也可以四處走動,但現在我堅持使用滴答方法。 我在 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;
}
}
當我像這樣調用這個方法時,玩家會像聲波一樣移動,並且它取決於玩家移動的速度。
我從我的世界 alpha 版本中得到了一些代碼,但我找不到調用tick()
方法的點。
我希望玩家以恆定的速度移動。
我還有一個計時器,我可以在其中獲取一些變量,例如 timepassed、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() 將使它在再次計算循環之前休息片刻。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.