I'm a beginner on java and android, and now I'm trying to program the "simon says" game to learn this language, but this doesn't works fine. :-(
The first and most important problem: When I try to show the sequence of colors [number 1 to 4] stored in the array seq[] with the method seq_show().... ho call but_show(x) to change the background color of each button... But I can't see the change of the backgroud color of the corresponding linearlayout... Also I has erased the READY? source code button because is not closed before seq_show().
I do not understand this java's way to program... is not line by line, step by step... I thing is by threads and I do not understand where the pointer of the executed code is. :-(
Can someone help me ?
Then, in the method seq_input() I catch the click's on the LinearLayout... and I thing this runs fine... but I need to solve the seq_show() problem first to continue with the program.
thanks !!
public class MainActivity extends Activity {
public LinearLayout l1, l2, l3, l4;
public GestureDetector gd1,gd2,gd3,gd4;
public static int seq[] = new int[25];
public int seq_pos = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Array pos[] amb 25 clics del joc simon
//
for (int i = 0; i < 25; i++) {
seq[i] = (int) (Math.random() * 4) + 1;
}
// declaració 4 onClickListener
//
l1 = (LinearLayout) findViewById(R.id.layout_green);
l1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
seq_input(1);
}
});
l2 = (LinearLayout) findViewById(R.id.layout_red);
l2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
seq_input(2);
}
});
l3 = (LinearLayout) findViewById(R.id.layout_yellow);
l3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
seq_input(3);
}
});
l4 = (LinearLayout) findViewById(R.id.layout_blue);
l4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
seq_input(4);
}
});
seq_show(0);
}
// Mostra la sequencia de botons a pulsar
//
public void seq_show(int seq_end) {
for (int i=0; i <= seq_end; i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
but_show(seq[i]);
}
}
// Espera la pulsació de la sequencia de botons
//
public void seq_input(int but_pushed){
if (seq[seq_pos] == but_pushed) {
seq_pos++;
but_show(seq[but_pushed]);
seq_show(seq_pos);
}else{
MediaPlayer sound = MediaPlayer.create(this, R.raw.wrong);
sound.start();
new Handler().postDelayed(new Runnable() {
public void run() {
}
}, 1000);
}
}
// Canvia el color i emet un so al botó but_num
//
public void but_show(int but_num) {
if (but_num == 1) {
l1.setBackgroundResource(color.green_on);
MediaPlayer sound = MediaPlayer.create(this, R.raw.tone_green);
sound.start();
new Handler().postDelayed(new Runnable() {
public void run() {
l1.setBackgroundResource(color.green_off);
}
}, 200);
} else if (but_num == 2) {
l2.setBackgroundResource(color.red_on);
MediaPlayer sound = MediaPlayer.create(this, R.raw.tone_red);
sound.start();
new Handler().postDelayed(new Runnable() {
public void run() {
l2.setBackgroundResource(color.red_off);
}
}, 200);
} else if (but_num == 3) {
l3.setBackgroundResource(color.yellow_on);
MediaPlayer sound = MediaPlayer.create(this, R.raw.tone_yellow);
sound.start();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
l3.setBackgroundResource(color.yellow_off);
}
}, 200);
} else if (but_num == 4) {
l4.setBackgroundResource(color.blue_on);
MediaPlayer sound = MediaPlayer.create(this, R.raw.tone_blue);
sound.start();
// SLEEP HERE ...
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
l4.setBackgroundResource(color.blue_off);
}
}, 200);
}
}
Bye !!