Skip to content Skip to sidebar Skip to footer

Is There An Approach Which Can Delay Certain Seconds Between Every Function Call Which Need To Update Ui Components?

I am writing a maze game, and one of its features is 'Show solution'. The solution may contain many steps, I am using a for loop to iterate every step, in order to show it clear to

Solution 1:

You can use a countdown timer.

 new CountDownTimer(30000, 1000){
                    public void onTick(long millisUntilFinished){
                        doUpdateOnEveryTick()
                    }
                    public  void onFinish(){
                       doActionOnFinish()
                    }
                }.start();

Solution 2:

Try something like this,

public void showSolution(View view) {
// Do something long
Runnable runnable = new Runnable() {
    @Override
    public void run() {
int stage = currentGame.currentStage;
    int[][] solution = GameMap.solutionList[stage];
    drawStage(stage);
    for(int[] step: solution) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            handler.post(new Runnable() {
                @Override
                public void run() {
                       ImageView v = (ImageView)findViewById(viewIdList[step[0]][step[1]]);
        setTimeout(() -> {v.callOnClick();}, 1);
                }
            });
        }
    }
};
new Thread(runnable).start();
}

Post a Comment for "Is There An Approach Which Can Delay Certain Seconds Between Every Function Call Which Need To Update Ui Components?"