
November 15th, 2012, 09:39 AM
|
|
Contributing User
|
|
Join Date: Oct 2011
Posts: 43
Time spent in forums: 10 h 31 m 45 sec
Reputation Power: 2
|
|
|
How to include Thread in while loop ?
I need to make the object change modes (movements) depending on the seconds elapsed. I created a while loop with while(object alive) ...
The thing is it loops way faster than seconds and goes into infinte loop. Someone suggested to include a Thread but I never worked with threads..some help or guidance is appreciated
Code:
private int seconds=0;
private boolean ghostalive=true;
protected static final int chaseMode = 0;
protected static final int scatterMode = 1;
protected static final int frightenedMode = 2;
static int mode = scatterMode; //initially ghost start in scatterMode
public Ghost(int x, int y, Maze maze){
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(){
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;
}
seconds++;
}
}
|