1
votes

I'm creating a simple game like the one known as the Simon Game. What it does is when I press the start button on the screen, it randomly creates 4 sequence of buttons (GREEN, RED, BLUE or YELLOW). The chosen colors are suppose to flash when the random generator creates them, but the flashing animation all happens at the end, after each Tone is played. I want to sync as the buttons are chosen, can anyone help me?

private OnClickListener startListenerS = new OnClickListener() {
    public void onClick(View v) {

         random = new Random();
         counter = 0;

         sequenceComparison = new int[4];


         Animation animation = new AlphaAnimation(1, (float) 0.5); // Change alpha from fully visible to invisible
         animation.setDuration(10); // duration - half a second
         animation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
         animation.setRepeatMode(Animation.REVERSE); // Reverse animation at the end so the button will fade back in

         Button buttonStart = (Button)findViewById(R.id.Button01);        
         buttonStart.setOnClickListener(startListener); // Register the onClick listener with the implementation above
         buttonStart.setBackgroundColor (Color.RED);  

         Button buttonStart1 = (Button)findViewById(R.id.Button02);        
         buttonStart1.setOnClickListener(startListener1); // Register the onClick listener with the implementation above
         buttonStart1.setBackgroundColor (Color.GREEN);

         Button buttonStart2 = (Button)findViewById(R.id.Button03);        
         buttonStart2.setOnClickListener(startListener2); // Register the onClick listener with the implementation above
         buttonStart2.setBackgroundColor (Color.YELLOW);

         Button buttonStart3 = (Button)findViewById(R.id.button1);        
         buttonStart3.setOnClickListener(startListener3); // Register the onClick listener with the implementation above
         buttonStart3.setBackgroundColor (Color.BLUE);

         sequenceArray = new int[sequenceTest];
         for(int i = 0; i < sequenceTest; i++) {
             int randnum = random.nextInt (sequenceTest) + 1;

             if(randnum == 1) {

                 sequenceArray[i] = 1; // THIS IS TO STORE THE CORRECT SEQUENCE IN THE ARRAY REPRESENTING RED BUTTON

                 //buttonStart.refreshDrawableState();
                 try {
                     playTone();
                     buttonStart.startAnimation(animation);
                     buttonStart.refreshDrawableState();
                     Thread.sleep(1000); // 1 second pause
                  } 
                  catch (InterruptedException e) {
                     e.printStackTrace();
                 } 
                 Thread.currentThread();
                 System.out.println("test Sequence Test: " + i + " " + sequenceArray[i]);
             }
             else if(randnum == 2) {
                 sequenceArray[i] = 2; // THIS IS TO STORE THE CORRECT SEQUENCE IN THE ARRAY REPRESENTING GREEN BUTTON
                 //buttonStart1.refreshDrawableState();
                 try {
                     playTone2();
                     buttonStart1.startAnimation(animation);
                     buttonStart1.refreshDrawableState();
                     Thread.sleep(1000); // 1 second pause
                  } 
                  catch (InterruptedException e) {
                     e.printStackTrace();
                  } 
                 Thread.currentThread();
                 System.out.println("test Sequence Test: " + i + " " + sequenceArray[i]);
             }

             else if(randnum == 3) {
                 sequenceArray[i] = 3; // THIS IS TO STORE THE CORRECT SEQUENCE IN THE ARRAY REPRESENTING YELLOW BUTTON
                 try {
                     playTone3();
                     buttonStart2.startAnimation(animation);
                     buttonStart2.refreshDrawableState();
                     Thread.sleep(1000);
                 } 
                 catch (InterruptedException e) {
                     e.printStackTrace();
                 }
                 Thread.currentThread();
                 System.out.println("test Sequence Test: " + i + " " + sequenceArray[i]);
             }
             else {
                 sequenceArray[i] = 4; // THIS IS TO STORE THE CORRECT SEQUENCE IN THE ARRAY REPRESENTING BLUE
                 try {
                     playTone4();
                     buttonStart3.startAnimation (animation);
                     buttonStart3.refreshDrawableState();
                     Thread.sleep(1000);
                 } 
                 catch (InterruptedException e) {
                     e.printStackTrace();
                 }
                 Thread.currentThread();
                 System.out.println("test Sequence Test: " + i + " " + sequenceArray[i]);
             }
         }
         // testing if the correct sequence is stored
         for (int i = 0; i < sequenceTest; i++) {
             System.out.println("Sequence Test: " + i + " " + sequenceArray[i]);

         }

    }
};
// *********************************** BUTTON CLICK LISTENER ***********************************
       private OnClickListener startListener = new OnClickListener() {
           public void onClick(View v) {

               Animation animation = new AlphaAnimation(1, (float) 0.5); // Change alpha from fully visible to invisible
               animation.setDuration(500); // duration - half a second
               animation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
               animation.setRepeatMode(Animation.REVERSE); // Reverse animation at the end so the button will fade back in 

               Button buttonStart = (Button)findViewById(R.id.Button01);        
               buttonStart.setOnClickListener(startListener); // Register the onClick listener with the implementation above
               buttonStart.setBackgroundColor (Color.RED);  
               buttonStart.startAnimation(animation);

               sequenceComparison[counter] = 1;
               System.out.println("red clicked" + sequenceComparison[counter]);
                playTone();      
                if(sequenceComparison[counter] == sequenceArray[counter]){
                    System.out.println("correct sequence");
                }
                else {
                    System.out.println ("Sorry, you clicked wrong");
                }
                counter++;
           }
       };
2

2 Answers

0
votes

First, beware of using Thread.Sleep() in the MainThread, always!

I found a previous answer that can suit your needs:

Android button animations synchronize with timing

Let me know!

0
votes

First, start your Animation instance using animation.start() after your animation.set() calls. Then, instead of using the startAnimation(animation) method, use the setAnimation(animation). This will synchronize any Views that use the animation.