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;
}
}
3 Replies - 221 Views - Last Post: 15 November 2012 - 12:01 PM
#1
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).
Replies To: Thread.sleep() in while loop doesn't work properly?
#2
Re: Thread.sleep() in while loop doesn't work properly?
Posted 15 November 2012 - 10:28 AM
What is that means
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
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
#3
Re: Thread.sleep() in while loop doesn't work properly?
Posted 15 November 2012 - 11:47 AM
pbl, on 15 November 2012 - 10:28 AM, said:
What is that means
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
/>
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.
#4
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
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
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|