while循环中的Thread.sleep()无法正常工作吗? [英] Thread.sleep() in while loop doesn't work properly?

查看:834
本文介绍了while循环中的Thread.sleep()无法正常工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该对象应该每5秒更改一次模式(运动算法).我首先尝试了一个while循环,但是循环太快了.然后,我添加了Thread.sleep(5000),但是我的对象仍然只能以一种算法(scatterMode)移动.这是算法:

The object is supposed to change modes (movement algorithm) every 5 seconds. I first tried with a while loop but the loop was iterating way too fast. Then I added Thread.sleep(5000) but still my object moves only in one algorithm (scatterMode). Here is the algorithm:

//LEVEL 1
//scatter for 7s 
//chase for 20s 
//scatter for 7s 
//chase for 20s 
//scatter for 5s 
//chase for 20s
//scatter for 5s 
//chase indefinite

这是代码.如果需要查看构造函数和变量声明,请在底部.

And here is the code. The constructor and variable declarations are at the bottom if you need to see them.

public void updateMode() throws InterruptedException {  
    while(ghostalive){
        if(seconds<7){
            Thread.sleep(100);
            mode = scatterMode;
        }
        if(7<seconds && seconds<27){
            Thread.sleep(5000);
            mode = chaseMode;
        }
        if(27<seconds && seconds<34){
            Thread.sleep(5000);
            mode = scatterMode;
        }
        if(34<seconds && seconds<54) {
            Thread.sleep(5000);
            mode = chaseMode;
        }
        if(54<seconds && seconds>59) {
            mode = scatterMode;
        }
        if(59< seconds && seconds<79){
            mode = chaseMode;
        }
        if(seconds>84){
            mode = scatterMode;
            ghostalive=false;
        }
        seconds++;
        ghostalive=false;
    }
}

private int seconds=0;
private boolean ghostalive=true;

protected static final int chaseMode = 0;
protected static final int scatterMode = 1;

static int mode = scatterMode; //initially ghost start in scatterMode

public Ghost(int x, int y, Maze maze) throws InterruptedException{
    super(x, y, maze);
    futureDirection = 0;
    timer = 0;
    updateMode();
    //chaseMode = false; 
    //frightenedMode = false;
}     

public static int getMode(){
    return mode;
}

推荐答案

您的睡眠模式是毫秒和几秒钟的混合,但是您希望数秒.

Your sleep pattern is mixture of milliseconds and several seconds, but you are expecting to count seconds.

尝试这样的事情:

while(ghostalive){

    if(seconds<7){
        mode = scatterMode;

    }

    if(7<seconds && seconds<27){
        mode = chaseMode;
    }

    if(27<seconds && seconds<34){
        mode = scatterMode;
    }

    if(34<seconds && seconds<54) {
        mode = chaseMode;
    }

    if(54<seconds && seconds>59) {
        mode = scatterMode;
    }

    if(59< seconds && seconds<79){
        mode = chaseMode;
    }

    if(seconds>84){
        mode = scatterMode;
        ghostalive=false;
    }

    seconds++;
    Thread.Sleep(1000);//Sleep for one second only

    //ghostalive=false; // Should this be here? Ghost is set to not alive after each loop?
}

我将睡眠转移到了if语句之后,以便在每个循环中保持一致.

I have moved the sleep after the if statements so that it is consistent in each loop.

这篇关于while循环中的Thread.sleep()无法正常工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆