I found it interesting and I decided to write the way it would solve this question.
NOT TESTED and lacks some tweaks, not least because some methods that I found to be most obvious were not written, but the bulk of the logic is here.
Come on.
Create a pointer variable, qte_sequence, and an array to store the sequences
global.
Create a Handler that receives messages to change button colors:
private Handler myHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
volta_a_cor_normal;
if ( ponteiro < qte_sequencia ) {
ponteiro++;
AcendeCor(ponteiro);
}
break;
case 5:
seta_a_cor_vermelha;
break;
case 10:
seta_a_cor_amarela;
break;
case 15:
seta_a_cor_azul;
break;
case 20:
seta_a_cor_verde;
break;
default:
break;
}
}
};
Initializes the objects, assigning each the click event to light
when the player touches the button. I'm using the DebouncedOnClickListener class
to avoid double click, but you can use the one that suits you.
btn_vermelho = (Button) findViewById(R.id.btnVermelho);
btn.setOnClickListener(new DebouncedOnClickListener() {
@Override
public void onDebouncedClick(View v) throws UnsupportedEncodingException {
}
myHandler.sendEmptyMessage(5);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
myHandler.sendEmptyMessage(0);
} ,1000);
}
});
...
Repeats for all others by changing what has to be changed.
Create the procedure that will turn on the buttons automatically.
private void AcendeCor(Integer ponteiro) {
Integer botao = sequencia[ ponteiro ];
switch (botão} {
1 : {
SetaBotaoAmarelo;
break;
}
2 : {
SetaBotaoVermelho;
break;
}
3 : {
SetaBotaoVerde;
break;
}
4 : {
SetaBotaoAzul;
break;
}
}
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
myHandler.sendEmptyMessage(0);
} ,1000);
}
}
Let's say I'm going to generate a 5-color sequence for the user to repeat
The randomly generated sequence was this:
1,2,2,4,3
Zero the variable pointer and arrow qte_sequencia = 5
I call the method AcendeCor( ponteiro )
The method receives the pointer, checks which button should light up accordingly
with the sequence. Lights up and initializes the Handler.
This in turn waits 1 second and calls myHandler which clears the button, increments the pointer, and repeats the process.