Runnable不会停止

时间:2016-04-19 17:55:57

标签: java android multithreading

我正在制作一个游戏,如果玩家与熊相交,它将停止可运行的线程。但不知怎的,它根本不会停止。所以当玩家与熊相交时,玩家应该死了。调用死循环,但不会停止runnable。 我制作了如下的可运行循环

protected void movement() {

    final int delay = 5;
    speed = 20;

    r = new Runnable() {

        @Override
        public void run() {

            if (countscore % 300 == 0) {
                speed = speed + 5;

            }

            bear1.setY(bear1.getY() + speed);
            bear2.setY(bear2.getY() + speed);
            reframeroad();

            if ((animImageView.getX() < bear1.getX() + 50 && animImageView.getX() > bear1.getX() - 50) ||
                    (animImageView.getX() > bear2.getX() && animImageView.getX() < bear2.getX())) {
                System.out.println("goes inside getx intersects x ");
                dead();

            }

            if (Rect.intersects(thiefRect, bear1Rect) || Rect.intersects(thiefRect, bear2Rect)) {
                System.out.println(" goes inside intersects if loop ");

                dead();  // this gets called
            }

            h.postDelayed(this, delay);
        }
    }; h.postDelayed(r, speed);
}

public void dead(){
    System.out.println("before callbacks null");
    h.removeCallbacksAndMessages(r);

    System.out.println("goes inside dead function");

    bear1.setVisibility(View.INVISIBLE);
    bear2.setVisibility(View.INVISIBLE);
    animImageView.setVisibility(View.INVISIBLE);


   scorebox.setText(countscore + "");
    scorebox.setVisibility(View.VISIBLE);
    score.setVisibility(View.VISIBLE);

    retry.setVisibility(View.VISIBLE);

    retry.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(GameActivity.this, GameActivity.class));
            finish();
        }
    });
}

我尝试了很多,但我仍会每隔5ms回电话。

我的控制台每次都像这样循环

> 04-19 10:36:40.687 14923-14923/: goes inside getx intersects x  
> 04-19 10:36:40.687 14923-14923/: before callbacks null 
> 04-19 10:36:40.687 14923-14923/: goes inside dead function 
> 04-19 10:36:40.747 14923-14923/: goes inside getx intersects x  
> 04-19 10:36:40.747 14923-14923/: before callbacks null 
> 04-19 10:36:40.747 14923-14923/:goes inside dead function 
> 04-19 10:36:40.807 14923-14923/: goes inside getx intersects x  
> 04-19 10:36:40.807 14923-14923/: before callbacks null 
> 04-19 10:36:40.807 14923-14923/: goes inside dead function 
> 04-19 10:36:40.868 14923-14923/: goes inside getx intersects x  
> 04-19 10:36:40.869 14923-14923/: before callbacks null 
> 04-19 10:36:40.869 14923-14923/: goes inside dead function

即使我的分数在调用死函数后继续增加

scorebox.setText(countscore + "");

如何停止此可运行线程?

1 个答案:

答案 0 :(得分:2)

在调用之前调用dead()函数:

h.postDelayed(this, delay);

尝试添加一个变量来指示它是否应该再次运行。

...
r = new Runnable() {

    @Override
    public void run() {

        if (countscore % 300 == 0) {
            speed = speed + 5;

        }

        bear1.setY(bear1.getY() + speed);
        bear2.setY(bear2.getY() + speed);
        reframeroad();

        boolean isDead = false;

        if ((animImageView.getX() < bear1.getX() + 50 && animImageView.getX() > bear1.getX() - 50) ||
                (animImageView.getX() > bear2.getX() && animImageView.getX() < bear2.getX())) {
            System.out.println("goes inside getx intersects x ");
            dead();
            isDead = true;
        }

        if (Rect.intersects(thiefRect, bear1Rect) || Rect.intersects(thiefRect, bear2Rect)) {
            System.out.println(" goes inside intersects if loop ");
            isDead = true;
            dead();  // this gets called
        }
        if (!isDead) {
            h.postDelayed(this, delay);
        }
   }
}; h.postDelayed(r, speed);


public void dead(){
    //Remove this --> h.removeCallbacksAndMessages(r);

    System.out.println("goes inside dead function");
    ....