1

Simon Game として知られるような単純なゲームを作成しています。画面のスタートボタンを押すと、ランダムに4つのボタンシーケンス(緑、赤、青、または黄色)が作成されます。選択された色は、ランダム ジェネレーターがそれらを作成するときに点滅すると想定されていますが、点滅するアニメーションはすべて、各トーンが再生された後の最後に発生します。ボタンが選択されたときに同期したいのですが、誰か助けてもらえますか?

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

2 に答える 2