0

I want to make a simple game, with one image view and two buttons to guess if the card is red of black.

I want to use a thread, for every 0.1 seconds before the player presses the button, the card is changing.

this is what i used so far :

Thread timer = new Thread() {
        public void run() {
            while (true) {
                try {
                    if(!isInterrupted())
                        sleep(100);
                    else
                        sleep(5000);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(!isInterrupted()) {
                                if (iv_card_to_Guess.getDrawable() == null)
                                    iv_card_to_Guess.setImageBitmap(deck_card);
                                else
                                    iv_card_to_Guess.setImageDrawable(null);
                            }
                            else {
//here need to update imageview with the actual image of the card, not just the deck or null
// for example 5 of Hearts

                                loadBitmap(getResourceID("img_" + numbers.get(count).toString(), "drawable", getApplicationContext()), iv_card_to_Guess);
                            }
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
    };

When i press a button i call timer.interrupt();

The application changes the image of the actual card but also for 0.1 seconds, not for 5 seconds, like i want :)

How can i do this, please?

cristi.gherghina
  • 311
  • 1
  • 6
  • 18

2 Answers2

0
     private Timer timer; 
      TimerTask task = new TimerTask() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
          insert the code you want to trigger here.
        }
    };
    timer = new Timer();

    int delay=5000;

    timer.schedule(task, delay); 
Vivek Khare
  • 162
  • 4
0

What you're doing introduces some uncertainties. I'm not sure about the exact implementation, but if isInterrupted() returns true and you call sleep(5000) an InterruptedException might be thrown immediately without any sleeping. Further the Runnable in the main Thread might run before the interrupted state is cleard, so that you card appears like intended, just to be removed on the next iteration of your while loop which only seeps for 0.1 seconds.

So instead you might better be using an Android animation for the blinking effect done with

if (iv_card_to_Guess.getDrawable() == null)
    iv_card_to_Guess.setImageBitmap(deck_card);
else
    iv_card_to_Guess.setImageDrawable(null);

Best introduce two methods startAnimation() and stopAnimation for that. You can find a guide for Animation and Graphics on Android.

With these you can stop the animation, when your button is clicked and start it again using View.postDelayed(run, delay) to give your card an exposure time of 5 seconds.

public void onClick(View v) {
    stopAnimation();
    loadBitmap(getResourceID("img_" + numbers.get(count).toString(), "drawable", getApplicationContext()), iv_card_to_Guess);
    iv_card_to_Guess.postDelayed(new Runnable() {
        startAnimation();
    }, 5000);
}
tynn
  • 38,113
  • 8
  • 108
  • 143