0

I'm doing this using handler.postdelayed but whenever i start clicking on tiles postdelayed doesn't work sometimes it comes fast and sometimes slow. Here is the code private Handler mhandler = new Handler();

private Runnable mcontinue = new Runnable() {
    @Override
    public void run() {

        //row5
        RockLocationRow5 = RockLocationRow4;
        setRockLocation(RockLocationRow5, 5);

        //row4
        RockLocationRow4 = RockLocationRow3;
        setRockLocation(RockLocationRow4, 4);

        //row3
        RockLocationRow3 = RockLocationRow2;
        setRockLocation(RockLocationRow3, 3);

        //row2
        RockLocationRow2 = RockLocationRow1;
        setRockLocation(RockLocationRow2, 2);

        //row1
        RockLocationRow1 = r.nextInt(3) + 1;
        setRockLocation(RockLocationRow1, 1);
        mhandler.postDelayed(this, 3000);

    }
};

I'm calling it in initgame function whenever the game starts and if i click on any tile I'm also calling this Runnable their

iv_13.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(RockLocationRow1 == 3){
                mcontinue.run();
       }
            else{
                //endGame();
            }
        }
    });

or is their anything else that I can use?? I'm a beginner...

this is how RockLocationRow is initialized

    //row3
    RockLocationRow3 = 1;
    iv_31.setImageResource(tapImage);
   
    //row2
    RockLocationRow2 = r.nextInt(3) + 1;
    setRockLocation(RockLocationRow2, 2);

    //row1
    RockLocationRow1 = r.nextInt(3) + 1;
    setRockLocation(RockLocationRow1, 1);

and this is setRockLocation

     private void setRockLocation(int place, int row){
        if(row == 1){
           iv_11.setImageResource(emptyImage);
           iv_12.setImageResource(emptyImage);
           iv_13.setImageResource(emptyImage);

        switch (place) {
            case 1:
                iv_11.setImageResource(tapImage);
                break;
            case 2:
                iv_12.setImageResource(tapImage);
                break;
            case 3:
                iv_13.setImageResource(tapImage);
                break;
        }
    }

same for row 2,3,4 and 5

1 Answers1

0

I think I found the problem. The problem is when you click a tile. If I have understood good, this code is this:

iv_13.setOnClickListener(new View.OnClickListener() {
  //rest of your code...
});

You see, when you click on a tile, you call the method run() of Runnable: mcontinue.run();

By calling this method, you execute AGAIN the postDelayed: mhandler.postDelayed(this, 3000); So, for every click on a tile, you execute the postDelayed. As a result, every 3 seconds FROM THE TIME YOU CLICKED A TILE, new tiles will be showing up. If you click lots of tiles, postDelayed will be executing many times and the tiles we be showing up fast.

A solution that may be correct, is to remove mcontinue.run(); when you click a tile. Like this:

iv_13.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(RockLocationRow1 == 3){
            }
            else {
                //endGame();
            }
        }
    });

Please, feel free to comment any of your thoughts/questions about this issue, and I will help as I can.
LoukasPap
  • 1,244
  • 1
  • 8
  • 17
  • nope I couldn't find setInterval() and clearInterval() in android studio :/ – CS_Student Jul 14 '20 at 05:59
  • @CS_Student Are you coding in java or javascript? – LoukasPap Jul 14 '20 at 08:20
  • @L.PapadopoulosJava – CS_Student Jul 14 '20 at 10:12
  • @CS_Student Then you should not have javascript as a tag :). setInterval and clearInterval are JavaScript methods. – LoukasPap Jul 14 '20 at 10:15
  • Do not worry, we all had first time :) – LoukasPap Jul 14 '20 at 10:58
  • @CS_Student did you check my solution? – LoukasPap Jul 15 '20 at 20:26
  • Yes in if condition when I'm removing mcontinue.run() I'm copying the code of run() in every click listener but even after that I'm getting same problem. And if I don't copy the code of run() in click listener then the tiles are moving after 3 seconds – CS_Student Jul 16 '20 at 07:32
  • Do you have a click listener for every tile? – LoukasPap Jul 16 '20 at 07:36
  • all tiles of last 3 Rows. Should I change? – CS_Student Jul 16 '20 at 07:46
  • I think that the generation of tiles(postDelayed), should run independently from the click of tiles. The only case that the generation should STOP, is Only if you click on a wrong tile. So, maybe you need to check the clicks of the wrong buttons, and If clicked, then stop the generations. Tell me if you understand what I mean, or I'll explain again. – LoukasPap Jul 16 '20 at 07:53
  • yes I have this condition that whenever someone clicks on empty tiles the game will finish. But I can't understand what you mean by post delayed should run independently from click of tiles now I've also removed mcontinue.run() from click listener function – CS_Student Jul 16 '20 at 08:03
  • Wait, I read in one of your previous comments that the tiles are moving after 3 seconds. I thought that this is what you wanted. Don't you want the tiles to move every 3 seconds? – LoukasPap Jul 16 '20 at 08:11
  • I want to move them every 3 sec but when user click on tile it should move instantly and shouldn't wait for 3 sec time – CS_Student Jul 16 '20 at 08:29
  • Then on every tile click, you should call setRockLocation for the last tile. One way, is to replace each rockLocation, with the next rockLocation (rockLocation+1). – LoukasPap Jul 16 '20 at 08:43
  • Lets try this. You will put postDelayed in a separate method from the one, that you replace the rockLocation. Then, you will call the method with the postDelay when you press the Play button. As a result, when you click the tiles, you will just replace the rockLocations and you will not trigger the postDelayed. – LoukasPap Jul 16 '20 at 10:55
  • won't it be same like when I removed the mcontinue.run() part and put the run() code(except postdelayed) part in click listener? – CS_Student Jul 16 '20 at 11:31
  • Yes. You will create a DIFFERENT play button that when you press it, it executes postDelay and the game will start. Then, every tile click will execute the mcontinue.run() (but the mcontinue.run() will not include postDelay) which will generate next tiles. – LoukasPap Jul 16 '20 at 19:29
  • so the play button which executes postdelay should it just call once this postdelay or should I put a loop till the game ends but then it will be same as mcontinue.run()... – CS_Student Jul 17 '20 at 15:02