5 Replies Latest reply: Jun 3, 2010 1:31 AM by 843789 RSS

    Need help with Thread inside TimerTask, Simon Game

    843789
      Hello 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.
        • 1. Re: Need help with Thread inside TimerTask, Simon Game
          Darryl Burke
          /removed

          Cross posted
          [http://www.codingforums.com/showthread.php?p=961174]

          I don't try to help cross posters.

          Edited by: DarrylBurke
          • 2. Re: Need help with Thread inside TimerTask, Simon Game
            843789
            Wow, I wasn't aware that this was inappropriate forum etiquette. Part of the reason I posted it here was because I realized I had accidentally posted it in Server Side Java on the other site. I was looking to reach a more general Java audience, perhaps the one marked New to Java even, by posting here.

            I did not see anything about this in the Code of Conduct which I did bother to read btw.

            Edited by: herpie39 on Jun 2, 2010 10:52 PM
            • 3. Re: Need help with Thread inside TimerTask, Simon Game
              843789
              herpie39 wrote:
              Wow, I wasn't aware that this was inappropriate forum etiquette.
              Not fourm etiquette. SImple common sense and courtesy.

              Imagine I walk up to you on the street and ask you for the time and before you have a chance to answer, I turn to another person and ask them and before they ask turn to yet another person and ask them etc etc. Contrast that to asking one person for the time and waiting for their reply.

              I don't know about you but I certainly would be pi$$ed off if someone did that to me.
              • 4. Re: Need help with Thread inside TimerTask, Simon Game
                843789
                You are comparing apples and oranges in my opinion. I am not speaking to just one person and then turning to another in the same vicinity. I directed questions at 2 rather vast groups of people that happen to be a mix of same/different people.

                As I already explained I felt that this forum would better reach my correct target audience.


                My apologies if I have broken an unwritten rule here, it was never my intention to offend anyone.
                • 5. Re: Need help with Thread inside TimerTask, Simon Game
                  843789
                  Also, if anyone can offer me assistance with my coding issue... You would be so awesome. I have been trying for several days every which way to succeed and have failed.

                  Please.

                  Sorry about the cross post thing.