所以我的問題是,我需要這些方法來運行一個又一個,但我不能解決如何使方法等待運行之前。 任何幫助表示讚賞。謝謝。 這裏是我的代碼:如何讓Java在繼續之前等待方法完成?
public void startMoving() throws InterruptedException
{
moveEnemy("right",3);
wait();
moveEnemy("down",3);
wait();
moveEnemy("right",2);
wait();
moveEnemy("up",1);
wait();
moveEnemy("right",2);
wait();
moveEnemy("up",2);
wait();
moveEnemy("right",2);
wait();
moveEnemy("down",4);
wait();
moveEnemy("left",1);
wait();
moveEnemy("down",2);
wait();
moveEnemy("right",3);
wait();
moveEnemy("up",2);
wait();
moveEnemy("right",1);
wait();
moveEnemy("up",1);
wait();
moveEnemy("right",3);
}
public void moveEnemy(final String direction, final int numMoves)
{
Thread moveThread = new Thread(new Runnable()
{
public void run()
{
isMoving = true;
int originalX = getX();
int originalY = getY();
for(int loop = 0; loop <= 98*numMoves; loop++)
{
try
{
Thread.sleep(5);
}
catch (InterruptedException e){}
if(direction.equals("up"))
{
setLocation(originalX,originalY+loop);
}
if(direction.equals("down"))
{
setLocation(originalX,originalY-loop);
}
if(direction.equals("left"))
{
setLocation(originalX-loop,originalY);
}
if(direction.equals("right"))
{
setLocation(originalX+loop,originalY);
}
}
try
{
Thread.sleep(50);
}
catch (InterruptedException e){}
notify();
}
});
moveThread.start();
'thread.wait()':
更多相關信息,在給定? – sircapsalot
很可能Thread.sleep(),我假設他想等待一段時間 – MWB
不要在UI線程中睡覺或等待... – assylias