简体   繁体   中英

Slow down colour sequence code

I'm currently creating a memory game that shows a random sequence of colors and you have to relay them back. It starts off showing 2 and then increases when you get it right. I'm using buttons that change colour but my problem is that they change far to quickly. I've tried event handlers but it's not slowing down the colours flashign up on the screen. I need some sort of delay. Here is the code to light a button depending on what random button id's have been placed in my array.

 public void PlaySequence()
{
    for(int i = 0; i< yourList.size();i++)
    {
        switch(Integer.parseInt(yourList.get(i).toString())) {
            case 0:

                redButton.setBackgroundColor(Color.RED);
                revertButtonColour(0);
                break;
            case 1:
                blueButton.setBackgroundColor(Color.BLUE);
                revertButtonColour(1);
                break;

            case 2:
                greenButton.setBackgroundColor(Color.GREEN);
                revertButtonColour(2);
                break;
            case 3:
                yellowButton.setBackgroundColor(Color.YELLOW);
                revertButtonColour(3);
                break;
        }
    }

EDIT:

I tried adding event handlers but they don't seem to be working

public void revertButtonColour(int number)
{
    int slowThisDown = 0;
    while(slowThisDown < 5000)
        slowThisDown++;

    switch(number)
    {
        case 0:
            Handler redHandler = new Handler();
            redHandler.postDelayed(new Runnable() {
                public void run() {
                    redButton.setBackgroundResource(android.R.drawable.btn_default);
                }
            }, sequenceSpeed);
            break;
        case 1:
            Handler blueHandler = new Handler();
            blueHandler.postDelayed(new Runnable() { public void run() {
                blueButton.setBackgroundResource(android.R.drawable.btn_default);}}, sequenceSpeed);
            break;
        case 2:
            Handler greenHandler = new Handler();
            greenHandler.postDelayed(new Runnable() { public void run() {
                greenButton.setBackgroundResource(android.R.drawable.btn_default);}}, sequenceSpeed);
            break;
        case 3:
            Handler yellowHandler = new Handler();
            yellowHandler.postDelayed(new Runnable() { public void run() {
                yellowButton.setBackgroundResource(android.R.drawable.btn_default);}}, sequenceSpeed);
            break;
    }
}

You could add a call to

Thread.Sleep (time_between_changes)

That's gonna mess up the UI thread though. Event handlers with a timer is probably the way to go.

EDIT:

Look up the Timer class and in the callback, send a message to the UI thread telling it which button to change. Below is just how to get the UI showing what you want it to.

    public void PlaySequence()
    {
        for(int i = 0; i< yourList.size();i++)
        {
            switch(Integer.parseInt(yourList.get(i).toString())) {
                case 0:

                    redButton.setBackgroundColor(Color.RED);
                    Thread.Sleep (2000);
                    redButton.setBackgroundResource(android.R.drawable.btn_default);
                    break;
                case 1:
                    blueButton.setBackgroundColor(Color.BLUE);
                    Thread.Sleep (2000);
                    blueButton.setBackgroundResource(android.R.drawable.btn_default);
                    break;

                case 2:
                    greenButton.setBackgroundColor(Color.GREEN);
                    Thread.Sleep (2000);
                    greenButton.setBackgroundResource(android.R.drawable.btn_default);
                    break;
                case 3:
                    yellowButton.setBackgroundColor(Color.YELLOW);
                    Thread.Sleep (2000);
                    yellowButton.setBackgroundResource(android.R.drawable.btn_default);
                    break;
            }
            Thread.Sleep (500);
        }
    }

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