我正在创建一个简单的游戏,比如名为 Simon Game 的游戏。它的作用是当我按下屏幕上的开始按钮时,它会随机创建 4 个按钮序列(绿色、红色、蓝色或黄色)。假设选择的颜色在随机生成器创建它们时会闪烁,但闪烁的动画都发生在最后,在播放每个 Tone 之后。我想在选择按钮时同步,有人可以帮我吗?
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++;
}
};