Need help with Thread inside TimerTask, Simon Game
843789Jun 3 2010 — edited Jun 3 2010Hello everyone. I'm fairly new to the Java language and programming in general. I've created a Simon game (like from the 80s with lights and sounds but a little different) and I am having some trouble with the light up animation for the computer player.
I have defined a class using the Runnable interface for my light up animation (which is really just flipping through jpegs with Thread.sleep delays in between). I have an instance of that class being used during the computer's turn and it's being called from an inner class for my TimerTask.
Something about the way I'm doing this is screwing up my animation. My logic model works fine and the score updates. I can even listen to the sounds each button makes after the computer's turn and know which buttons were used. Here is a snippet of my call to the animation thread (which works fine by itself tied to an actionListener btw).
private void computerTurn()
{
int delay = 100;
int repeat = 1000;
boolean testForEnd = true;
internalCount = 0;
stop = false;
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
String color = brain.randomColor();
colorShift(color);
colorSound(color);
brain.incrementColorRecord("computer", color);
internalCount++;
if(internalCount >= brain.returnLevelInt())
stop = true;
}}, delay, repeat);
do
{
if(stop)
{
timer.cancel();
testForEnd = false;
}
}
while(testForEnd);
}
Can someone suggest a better way to do what I am trying to do? Is it poor practice to nest a Thread? I guess that is what I am doing since TimerTask implements Runnable.
I have included a full copy of my source and images/sounds on Microsoft Sky Drive. I would uploaded it here but the limit is 50k. The zip file is small still, 500k. http://cid-f9b6546bcf7e9fda.skydrive...px/Public?uc=1
P.S. After you select new game and copy the computer for the first round hit continue to advance to the next stage. The animation will work fine the first stage. The light up animation always works for the last button in the sequence, for example, if on round 7 the 7th.