我正在用骰子和一个移动的棋子做一个游戏。我想要的是掷骰子,然后在掷完骰子后,我想让它移动。我目前有当骰子结束滚动时,骰子对象告诉骰子开始移动,但是我想要一个控制器来告诉骰子移动,并等待他们完成,然后告诉骰子移动。我尝试过使用.wait()和.notify(),但我真的不知道如何使用它们,最终得到了一个InterruptedException。实现这一点的最佳方式是什么?
发布于 2014-06-09 16:27:42
将一个javax.swing.Timer
用于骰子,另一个用于骰子;在骰子处理程序中,当您确定骰子已完成时,启动骰子计时器。有几个例子在here中得到了检验。
发布于 2014-06-09 16:24:30
你可能想看看How to Pause and Resume a Thread in Java from another Thread。
似乎你不能使用任何其他方法,但发帖者建议在那里暂停一个帖子。他使用变量来知道何时运行或暂停。举个例子:
public class Game
{
static Thread controller, dice;
static boolean dicerunning = false;
public static void main(String[] args)
{
controller = new Thread(new Runnable()
{
public void run()
{
dicerunning = true;
dice.start();
while (dicerunning)
{
//blank
}
//tell piece to move here
}
});
dice = new Thread(new Runnable()
{
public void run()
{
//roll here
dicerunning = false;
}
});
controller.start();
}
}
https://stackoverflow.com/questions/24124119
复制相似问题