简体   繁体   中英

change image of imagebutton

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 !

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) {

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.

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) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM