2016-04-19 174 views
0

我正在製作一個遊戲,如果玩家與熊相交,它將停止可運行的線程。但不知何故,它不會停下來。所以當玩家與熊相交時,玩家應該死亡。死循環被調用,但不會停止可運行。 我做了可運行的循環,如下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 + ""); 

如何停止該運行的線程?

+0

你在哪裏'.start()'新的可運行?可能是因爲我的活動一開始,就會通過主線程作爲普通函數調用 –

+0

運行它,可啓動運行。 。從未做過任何.start() – Pujan

+1

@Gilad - runnable通過處理程序啓動..... – Opiatefuchs

回答

2

你死()函數在調用之前調用:

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"); 
    .... 
+0

這工作。非常感謝先生。 – Pujan

+0

其常見做法,使運行檢查布爾變量'揮發性',以確保他們的翻轉是所有正在運行的線程 –