I am making a game with dice and a moving piece. What I want is the dice to roll and then after the dice are finished rolling i want the piece to move. I currently have when the dice finish rolling the dice object tells the piece to start moving however I want a controller to tell the dice to move and wait for them to finish then tell the piece to move. I have tried using .wait() and .notify() but I do not really know how to use them and end up getting an InterruptedException. What is the best way to implement this?
Asked
Active
Viewed 41 times
0
-
I think it depends on how you implemented your application (sounds like MVC). My suggestion: The controller calls the method roll and afterwars sets the new position of the model (piece). Your View is the observer of the model and gets updated on every change. To be viewable by human use javax.swing.Timer. – Thomas Jun 09 '14 at 16:28
2 Answers
1
Use one javax.swing.Timer
for the dice and another for the piece; in the dice handler, when you determine that the dice are finished, start the piece timer. Several examples are examined here.
0
You might want to see How to Pause and Resume a Thread in Java from another Thread.
It seems you can't use any other way but the poster suggested there, to pause a thread. He used variables to know when to run or pause. So for example:
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();
}
}

Community
- 1
- 1

godlovesdavid
- 169
- 2
- 5