0

I have a class extending Thread. Inside this run I have a few method working in while loop. Now I want one of this methods gameSurface.updateBitmapObjects() to work with delay. How to achieve this? Here is my code:

GameThread.java:

public class GameThread<T extends GameSurface> extends Thread {

    private boolean isRunning;
    private long startTime, loopTime;
    private long delay = 33;
    private SurfaceHolder surfaceHolder;
    private T gameSurface;

    public GameThread(SurfaceHolder surfaceHolder, T gameSurface)  {
        this.surfaceHolder= surfaceHolder;
        this.gameSurface = gameSurface;
        isRunning = true;
    }

    @Override
    public void run()  {
        while(isRunning) {
            startTime = SystemClock.uptimeMillis();
            Canvas canvas = surfaceHolder.lockCanvas(null);
            if(canvas != null) {
                synchronized (surfaceHolder) {

                    gameSurface.updateBitmapObjects();
                    gameSurface.drawMapBitmap(canvas);
                    ((GameMainSurface)gameSurface).drawCoinAndCoins();


                    ((GameMainSurface)gameSurface).drawFieldLines(canvas);

                    ((GameMainSurface)gameSurface).updatePlayersLabels(canvas);
                    if (((GameMainSurface)gameSurface).isGameMainFragment()) {
                        ((GameMainSurface)gameSurface).setSomeValues();
                    }


                    gameSurface.drawObjects(canvas);


                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }

            loopTime = SystemClock.uptimeMillis() - startTime;

            if(loopTime < delay) {
                try {
                    Thread.sleep(delay - loopTime);
                }
                catch (InterruptedException e) {
                    Log.e("Interupted ex", e.getMessage());
                }
            }
        }
    }
}

GameMainSurface.java:

    (...)
    public void updateBitmapObjects() {
        this.chibi1.update();
        (...)
    }

MovingObject.java:

    // I want this method to work with delay let's say 100 ms

    public void update() {
        this.colUsing++;
        if (colCount == 13) {
            if (colUsing >= 9) {
                if (rowUsing == ROW_TOP_TO_BOTTOM_CHIBI || rowUsing == ROW_BOTTOM_TO_TOP_CHIBI)
                    colUsing = 1;
                else
                    colUsing = 0;
            }
        }
        if (colCount == 4) {
            if (colUsing >= 4) {
                this.colUsing = 0;
            }
        }
    }
DiLDoST
  • 335
  • 3
  • 12
  • Please check if this gives you some inspiration: https://stackoverflow.com/questions/61023968/what-do-i-use-now-that-handler-is-deprecated – Enowneb Feb 06 '23 at 01:53

0 Answers0