3 Replies - 876 Views - Last Post: 15 November 2012 - 12:01 PM Rate Topic: -----

#1 hey92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 07-November 11

Thread.sleep() in while loop doesn't work properly?

Posted 15 November 2012 - 10:17 AM

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).


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

//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

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




Is This A Good Question/Topic? 0
  • +

Replies To: Thread.sleep() in while loop doesn't work properly?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Thread.sleep() in while loop doesn't work properly?

Posted 15 November 2012 - 10:28 AM

What is that means

Quote

but still my object moves only in one algorithm (scatterMode).

What do you mean my object moves ?

who calls that method ? Not the paint() method I hope :)

Who is accessing mode or getMode() ? Not another Thread I hope :)
Was This Post Helpful? 0
  • +
  • -

#3 hey92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 07-November 11

Re: Thread.sleep() in while loop doesn't work properly?

Posted 15 November 2012 - 11:47 AM

View Postpbl, on 15 November 2012 - 10:28 AM, said:

What is that means

Quote

but still my object moves only in one algorithm (scatterMode).

What do you mean my object moves ?

who calls that method ? Not the paint() method I hope :)/>

Who is accessing mode or getMode() ? Not another Thread I hope :)/>


No i have a bunch of other classes that control the movement that work fine. It just that Im not able to switch btw modes. Someone suggested using Thread.sleep(1000) but still no difference. Im stuck with the same mode of movement.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Thread.sleep() in while loop doesn't work properly?

Posted 15 November 2012 - 12:01 PM

I'm sorry but by what we can see...
you have a

while(ghostAlive)
inside that you switch the mode based on second
then you increment second
you set ghostAlive false and you return

so what is the use of the while() as it will be excuted only once ?
you start it because to set ghostAlive to true
you switch to set de mode
you add 1 to the second
you set ghostAlive to false so you exit the while
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1