Como faço para o Java aguardar a conclusão de um método antes de continuar?
Portanto, meu problema é que preciso desses métodos para executar um após o outro, mas não consigo descobrir como fazer com que os métodos esperem antes de serem executados. Qualquer ajuda é apreciada. Obrigado. Aqui está o meu código:
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();