0

I'm begining on android and java.

I try to make a simon game but have some problems.

I wrote this to show the simon buttons sequence or the button pushed by the player:

    if (but_num == 1) {
        ib1.setImageResource(R.drawable.bullet_square_green);
        MediaPlayer sound = MediaPlayer.create(this, R.raw.tone_green);
        sound.start();
        for (int x = 1; x < 10000000; x++) { };
        ib1.setImageResource(R.drawable.bullet_ball_green);
    } else if (but_num == 2) {

It should change the image of each imagebutton, play a sound, wait some time (for {}) and then change the image again....

But it doesn't work well... it plays the sound and really changes the image by bullet_square_xxx, but the eye can't see the image change, the change is only visible if the image is not changed back again by the bullet_ball_xxx :-(

I think this is my fault because I wrote the code different than java really works... I'm a beginner and don't think in java... I have the visual basic program structure on my mind yet.

Thank You and sorry for my English !

Kasper van den Berg
  • 8,951
  • 4
  • 48
  • 70
ev3c
  • 11
  • 1
  • 2

3 Answers3

0

This is probably caused by a delay on the event dispatch thread and the fact that the empty loop might be even ignored by the compiler since it is static, it is easily predicted to have no effect on the program. My suggestion is first force a repaint/update on the GUI and use Thread.sleep. Something like this:

if (but_num == 1) {
    ib1.setImageResource(R.drawable.bullet_square_green);
    updateUI(); // if you are somewhere in a class extending any Frame/Panel
    //If you are in other class use mainFrame.repaint(); 
    MediaPlayer sound = MediaPlayer.create(this, R.raw.tone_green);
    sound.start();
    try{
        Trhead.sleep(3000);
    } catch (InterruptedException e) {}
    ib1.setImageResource(R.drawable.bullet_ball_green);
    updateUI(); //only if this effect is delayed too
} else if (but_num == 2) {
Alex Botev
  • 1,369
  • 2
  • 19
  • 34
0

ok....I think delay is the problem in your code. Since nowadays there are highspeed processors available that can count to 10000000 in a few ms, mine does. So instead of using the old-school for loop to introduce a delay use

Thread.sleep(5000);

this causes a delay of 5 sec, the argument is the time in milliseconds.

Shivam Shah
  • 524
  • 5
  • 10
  • I try to use thread.sleep() and the image don't change. :-( Maybe is better for the simon say game work only with the background color of an image or framerelay. – ev3c Jul 28 '12 at 14:04
0

there is another thread which talks about introducing a delay: How to pause / sleep thread or process in Android?

you could try this [i have copied pasted from that thread]:

 if (but_num == 1) {
    ib1.setImageResource(R.drawable.bullet_square_green);
    MediaPlayer sound = MediaPlayer.create(this, R.raw.tone_green);
    sound.start();

    // SLEEP 2 SECONDS HERE ...
Handler handler = new Handler(); 
handler.postDelayed(new Runnable() { 
     public void run() { 
         ib1.setImageResource(R.drawable.bullet_ball_green);
     } 
}, 2000);


} else if (but_num == 2) {
Community
  • 1
  • 1
Srikanth
  • 306
  • 1
  • 2
  • 10
  • This runs... but the change of the image is not very fine... I'm thinking about use only the background color (normal or highlighter) of an image or frame relay... maybe is better for the "simon says" game... – ev3c Jul 28 '12 at 14:06