2012-12-03 97 views
1

我創建一個簡單的遊戲,像一個被稱爲西蒙遊戲。它的功能是當我按下屏幕上的開始按鈕時,它隨機創建4個按鈕序列(綠色,紅色,藍色或黃色)。當隨機生成器創建它們時,所選擇的顏色會閃爍,但閃爍的動畫都會在每個Tone播放後的最後發生。我想在選擇按鈕時進行同步,任何人都可以幫助我嗎?同步我的Android動畫

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++; 
      } 
     }; 

回答

0

首先,在調用animation.set()之後,使用animation.start()開始動畫實例。 然後,使用setAnimation(animation)而不是使用startAnimation(animation)方法。 這將同步任何使用動畫的視圖。