Pujan Pujan - 7 months ago 40
Java Question

Runnable wont stop

I am making a game where if the player intersects with bear, it will stop the runnable thread. but somehow it wont stop at all. So when player intersects with bear, the player should be dead. the dead loop is called but it will not stop the runnable.
I made the runnable loop as 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(); // 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();
}
});
}


I tried a lot but I will still call back on every 5ms.

My console loops like this 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 the my score keeps on increasing after the dead function is called

scorebox.setText(countscore + "");


How do i stop this runnable thread?

Answer

Your dead() function is called before calling:

h.postDelayed(this, delay);

Try adding a variable to indict if it 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();  // 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");
    ....