0

I am new to Android Development and I am trying to make a game. I have a 3x3 grid of imagebuttons and a play button. When the user clicks the play button, a number of buttons should light up and then turn off. That works fine. Now I am trying to have the buttons light up and turn off one by one. So if comp_blocks contains [1, 3, 2] button 1 would light up, then turn off, then button 3 would light up.... etc. What is the best way to do this? I would like the button to stay lit for a half a second.

Edit: To be clear, I want each one to light up and go out all on one press of the button. Press button, display 1, turn off 1, display 3, turn off 3. Next time the user presses the button, a new sequence would show.

        //Create pattern in comp_blocks
        for(int i = 0; i < move_num+2; i++){
            comp_blocks.add(rand.nextInt(9) + 1);
        }

        //Display pattern
        ListIterator<Integer> itr = comp_blocks.listIterator();
        while (itr.hasNext()) {
            final Integer button_show = itr.next();
            switch (button_show){
                case 1:
                    button1.setBackgroundColor(getResources().getColor(R.color.garnet));
                    break;
                case 2:
                    button2.setBackgroundColor(getResources().getColor(R.color.garnet));
                    break;
                case 3:
                    button3.setBackgroundColor(getResources().getColor(R.color.garnet));
                    break;
                case 4:
                    button4.setBackgroundColor(getResources().getColor(R.color.garnet));
                    break;
                case 5:
                    button5.setBackgroundColor(getResources().getColor(R.color.garnet));
                    break;
                case 6:
                    button6.setBackgroundColor(getResources().getColor(R.color.garnet));
                    break;
                case 7:
                    button7.setBackgroundColor(getResources().getColor(R.color.garnet));
                    break;
                case 8:
                    button8.setBackgroundColor(getResources().getColor(R.color.garnet));
                    break;
                case 9:
                    button9.setBackgroundColor(getResources().getColor(R.color.garnet));
                    break;
            }
easyxtarget
  • 95
  • 1
  • 1
  • 10

1 Answers1

1

You could use a Runnable like this

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    //Do something after 100ms
  }
}, 100);

(code taken from How to call a method after a delay in Android)

Another solution would be to create a class that extends the buttonview with a variable containing the time the button was last pressed, and create un update method that checks if the time limit elpased. Then iterate over the buttons calling the update method. The second option seems safer for me, as it won't create new tasks everytime you click the button.

EDIT:

From what I understand you have a main loop for this game, add something like this in it :

Time now=new Time;
Time lastupdate=new Time;
while(yourcondition)/*this is your main loop*/
{
    /*your stuff*/

        now.setToNow();
        if(now.toMillis(false)>lastupdate.toMillis(false)+TimeToWaitInMilliSeconds)
        {
            if(!buttonstolit.isEmpty()) {
                resetTheButtons();
                lightThisButton(buttonstolit.get(0));
                buttonstolit.remove(0);
                lastupdate.setToNow();
            }
        }

}

Where buttonstolit is declared as List.

Use buttonstolit.add(TheButtonToLitNext); to add buttons to the waiting list. You also need to clear the list before a new sequence (or check if it is empty and wait for another button to be pressed)

I assume that you are here using integers to select the buttons (which seems to be the case from your code)

Community
  • 1
  • 1
Lectem
  • 503
  • 6
  • 19
  • I have tried this but it doesn't seem to work. What would I put inside the run function? To be clear, I want each one to light up and go out all on one press of the button. Press button, display 1, turn off 1, display 3, turn off 3. Next time the user presses the button, a new sequence would show. – easyxtarget Jul 06 '14 at 20:33
  • Why not use a list containing the sequence, and have an object that checks the time and pick the 1st element out of the list and update the screen then ? – Lectem Jul 06 '14 at 21:05