有线程间通信时遇到问题 [英] Having trouble communicating between threads

查看:122
本文介绍了有线程间通信时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在两个构建一个游戏,使用多线程的第一次尝试是要多为好,但我坚持的时刻。

My first attempt at both building a game and using multithreading is going mostly well, but I'm stuck at the moment.

这是一个简单的打地鼠克隆,所以我有一个layout.xml宣布摩尔ImageViews,然后我使用的3×4格的setContentView(R.layout.layout)放起来,然后一个单独的线程,使他们中的一个出现了一秒钟,然后消失。这里是我的Activity的的onCreate()

It's a simple Whack a mole clone, so I've got a 3x4 grid of mole ImageViews declared in a layout.xml, then I'm using setContentView(R.layout.layout) to put it up, then a separate thread to make one of them appear for a second, then disappear. Here's my Activity's onCreate():

public class WAM_Activity extends Activity {

private static final int MAKE_VISIBLE = 1;
private static final int MAKE_INVISIBLE = 0;
private ImageView[] mole = new ImageView[11];
private ImageView currentMole;
private int[] moleId = {R.id.mole1, R.id.mole3, R.id.mole4, R.id.mole5, R.id.mole6, R.id.mole7, R.id.mole8, R.id.mole9, R.id.mole10, R.id.mole11, R.id.mole12};
private boolean running = true;
private int randomInt = 0;
private Random rand = new Random();
private Handler handler;
//private WAM_Thread wamthread = new WAM_Thread();
private Context cont = this;
private static Handler h;
Message msg;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.wam_view_layout);

    //add ImageViews declared in R.layout.wam_view_layout to ImageView objects
    for (int i = 0; i < 11; i++) {
        mole[i] = (ImageView) findViewById(moleId[i]);
        mole[i].setVisibility(View.INVISIBLE);
        mole[i].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(cont, "You clicked one!", Toast.LENGTH_SHORT).show();
            }
        });
    }

    handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what) {
                case MAKE_VISIBLE:
                    currentMole = (ImageView) msg.obj;
                    currentMole.setVisibility(View.VISIBLE);
                    break;
                case MAKE_INVISIBLE:
                    currentMole = (ImageView) msg.obj;
                    currentMole.setVisibility(View.INVISIBLE);
            }
        }
    };

    Runnable runnable = new Runnable() {
        public void run() {
            while (running) {
                randomInt = rand.nextInt(11);
                msg = handler.obtainMessage();
                msg.obj = mole[randomInt];
                msg.what = MAKE_VISIBLE;
                handler.sendMessage(msg);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        msg = handler.obtainMessage();
                        msg.obj = mole[randomInt];
                        msg.what = MAKE_INVISIBLE;
                    }
                }, 1000);
            }
        }
    };
    Thread thread = new Thread(runnable);
    thread.start();
}

所有的痣被宣布在默认情况下在布局xml文件不可见,所以这code,应使他们中的一个可见的,等待一秒钟,然后让他再次隐形,然后再拍可见,重复动作。相反,它使他们所有可见所有的时间。他们还回应水龙头,但仅此而已。

All the moles are declared invisible by default in the layout xml file, so this code should be making one of them visible, wait a second, then make him invisible again and then make another visible and repeat. Instead, it's making them all visible all the time. They still respond to taps, but that's it.

任何人都看到我在做什么错?我一直在这个挣扎,因为昨晚但我真的很接近得到它的权利。

Anyone see what I'm doing wrong? I've been struggling with this since last night but I'm really close to getting it right.

推荐答案

两件事情:


  • 在您可运行的末尾添加视频下载(1000)让后台线程使后暂停一件事可见

  • 里面的postDelayed可运行的,你不叫 handler.sendMessage

  • at the end of your runnable, add a Thread.sleep(1000) so that the background thread pauses after making one thing visible
  • inside your postDelayed runnable, you're not calling handler.sendMessage

这篇关于有线程间通信时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆