java - Runnable wont stop -


i making game if player intersects bear, stop runnable thread. somehow wont stop @ all. when player intersects bear, player should dead. dead loop called not stop runnable. made runnable loop below

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

i tried lot still call on every 5ms.

my console loops every time

> 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 

even score keeps on increasing after dead function called

scorebox.settext(countscore + ""); 

how stop runnable thread?

your dead() function called before calling:

h.postdelayed(this, delay); 

try adding variable indict if should run again.

... 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();  // gets called         }         if (!isdead) {             h.postdelayed(this, delay);         }    } }; h.postdelayed(r, speed);   public void dead(){     //remove --> h.removecallbacksandmessages(r);      system.out.println("goes inside dead function");     .... 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -