简体   繁体   English

减慢颜色序列代码

[英]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.它开始显示 2,然后在你做对时增加。 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.这是根据我的数组中放置的随机按钮 id 来点亮按钮的代码。

 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.但这会弄乱 UI 线程。 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.查找 Timer 类并在回调中,向 UI 线程发送一条消息,告诉它要更改哪个按钮。 Below is just how to get the UI showing what you want it to.下面是如何让 UI 显示您想要的内容。

    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);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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