Adding a delay without Thread.sleep and a while loop doing nothing

前端 未结 3 742
灰色年华
灰色年华 2021-01-05 02:39

I need to add delay without using Thread.sleep() or a while loop doing nothing. The game im editing(Minecraft) clock runs on \"Ticks\" but they can fluctuate depending on yo

3条回答
  •  暖寄归人
    2021-01-05 03:23

    Something like the following should give you the delay you need without holding up the game thread:

    private final long PERIOD = 1000L; // Adjust to suit timing
    private long lastTime = System.currentTimeMillis() - PERIOD;
    
    public void onTick() {//Called every "Tick"
        long thisTime = System.currentTimeMillis();
    
        if ((thisTime - lastTime) >= PERIOD) {
            lastTime = thisTime;
    
            if(variable) { //If my variable is true
                boolean = true; //Setting my boolean to true
                /**
                *Doing a bunch of things.
                **/
                //I need a delay for about one second here.
                boolean = false; //Setting my boolean to false;
            }
        }
    }
    

提交回复
热议问题