简体   繁体   English

如何在java中的for循环中延迟方法?

[英]How do I delay a method in a for loop in java?

I am having trouble delaying the method assign_backgrounds() within a for loop. 我在for循环中延迟方法assign_backgrounds()时遇到问题。 I am trying to create a Simon says game, but instead of delaying and showing the next button that "Simon" presses, it shows all the buttons at once . 我正在尝试创建一个西蒙说的游戏,但不是延迟并显示“西蒙”按下的下一个按钮,它会立即显示所有按钮 Any help here would be greatly appreciated. 这里的任何帮助将不胜感激。 Thanks. 谢谢。

boolean simonsTurn = true;
int x = 4;
int s;
int delay = 1000;
int array_values[] = new int[]{1,2,3,4};


public void simonSays() {
    // running = true;
    if (simonsTurn == true) {
        go();

        for (int i = 0; i < x; i++) {
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                public void run() {
                    go();
                }
            }, 1000);
        }            
    }
}

public void go(){
    s = random_int_between(0,3);
        assign_backgrounds(array_values[s]);
}

public void assign_backgrounds( int array_values ){

    Handler handler = new Handler();

    if( array_values == 1){
        button1_.invalidate();
        button1_.setBackgroundResource(R.drawable.goatclicked);
        button1_.refreshDrawableState();

        handler.postDelayed(new Runnable(){
        public void run(){
        button1_.invalidate();
        button1_.setBackgroundResource(R.drawable.goat);
        button1_.refreshDrawableState();}}, 1000);
        }
    else if( array_values == 2){
        button2_.invalidate();
        button2_.setBackgroundResource(R.drawable.pigclicked);
        button2_.refreshDrawableState();

        handler.postDelayed(new Runnable(){
            public void run(){
                button2_.invalidate();
                button2_.setBackgroundResource(R.drawable.pig);
                button2_.refreshDrawableState();}}, 1000);
    }
    else if( array_values == 3){
        button3_.invalidate();
        button3_.setBackgroundResource(R.drawable.chickenclicked);
        button3_.refreshDrawableState();

        handler.postDelayed(new Runnable() {
            public void run() {
                button3_.invalidate();
                button3_.setBackgroundResource(R.drawable.chicken);
                button3_.refreshDrawableState();}}, 1000);
    }
    if( array_values == 4) {
        button4_.invalidate();
        button4_.setBackgroundResource(R.drawable.cowclicked);
        button4_.refreshDrawableState();

        handler.postDelayed(new Runnable(){
            public void run(){
                button4_.invalidate();
                button4_.setBackgroundResource(R.drawable.cow);
                button4_.refreshDrawableState();}}, 1000);
    }
}

It's because you are creating handlers very fast and then they are all starting at the same time. 这是因为你正在快速创建处理程序,然后它们都在同一时间开始。 You should look into how Handler's work and about Asyncronous/Background tasks. 您应该了解Handler的工作方式以及Asyncronous / Background任务。

Now back to your problem, you are calling the a loop and it is creating handlers all in a row and they are being created very fast (nanoseconds). 现在回到你的问题,你正在调用一个循环,它正在连续创建处理程序,并且它们的创建速度非常快(纳秒)。 They will then all launch 1 second from that creation time because of your postDelayed() call. 然后,由于你的postDelayed()调用,它们将从创建时间开始1秒钟。 This is why everything is popping up at the same time! 这就是为什么一切都在同时出现的原因! All of these delay posts are being executed at almost the same time on concurrent background threads. 所有这些延迟帖子几乎同时在并发后台线程上执行。

Instead of a for(int i,...) loop you want to have a global int i , just add it to the top of the file. 而不是for(int i,...)循环,你想要一个全局int i ,只需将它添加到文件的顶部。

At the end of any of Simon's turn you'll want, inside of the if, else if statement inside assign_background (at the end of the Runnables, then you'll want to call go() . 在Simon的任何一个结束时你都会想要,在assign_background里面的if, else if语句里面(在Runnables的末尾,你会想要调用go()

This might cause problems because you are trying to access the main thread from all these background threads. 这可能会导致问题,因为您尝试从所有这些后台线程访问主线程。 so you might have to call the function runOnMainUIThread() as a quick hack when you call the go function. 所以你可能不得不在调用go函数时将函数runOnMainUIThread()调用为快速黑客。

All in all, you are going to have some problems until you understand Handlers, Background Processes, and Threads. 总而言之,在了解处理程序,后台进程和线程之前,您将遇到一些问题。 Definitely great knowledge to learn about and Android has solid documentation on it just FYI. 绝对有很好的知识可以学习,Android只有FYI才有可靠的文档。

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

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