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

There are 1 best solutions below

18
LoukasPap On

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.