對象應該根據流逝的時間(在追逐或分散之間切換)更改模式(移動算法)。我創建了一個while循環,但對象只在一種模式下(追逐)移動,這很奇怪,因爲我將它設置爲初始分散。雖然循環無法正常工作
private static int seconds=0;
private static boolean ghostalive;
protected static final int chaseMode = 0;
protected static final int scatterMode = 1;
protected static final int frightenedMode = 2;
static int mode; //initially ghost start in scatterMode
public Ghost(int x, int y, Maze maze){
super(x, y, maze);
futureDirection = 0;
timer = 0;
mode = getMode();
}
public static int getMode(){
mode=setMode();
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 static int setMode(){
while(ghostalive){
mode = scatterMode;
if(seconds>7)
mode = chaseMode;//chaseMode=true;
if(seconds>27)
mode = scatterMode;
if(seconds>34)
mode = chaseMode;
if(seconds>54)
mode = scatterMode;
if(seconds>59)
mode = chaseMode;
if(seconds>79)
mode = scatterMode;
if(seconds>84)
mode = chaseMode;
seconds++;
}
return mode;
}
'ghostAlive'永遠不會被初始化。如果是這樣,這個「while」循環將是無限的,因爲它永遠不會更新。 – squiguy