I'm working on a game that at some point needs to run for x steps to update certain objects on the screen. As of now, all that's seen by the player is step 0, and step x. How do i show each step in-between for a set time?
I've tried adding a check to make sure onDraw() is called in-between, as well as sleeps, making sure to call PostInvalidate after each step is done in hopes of forcing onDraw() to be called, but haven't had any luck.
(ex. In step function, have a bool "nextStep" that is set to false at the end of the function, onDraw() sets this to true at the end. If it's false, step function will wait to update the 2d array of objects that gets drawn to the screen until "ondraw" sets the bool to true.)
This works
for (int i = 0; i < steps; i++) {
// postDelayed fixes this because it tells the GPU to draw on next cycle otherwise, it has already passed the draw by now.
postDelayed(new Runnable() {
@Override
public void run() {
Canvas c = null;
step(CellType.PLAYER);
invalidate();
try {
c = getHolder().lockCanvas();
synchronized (getHolder()) {
draw(c);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (c != null) {
getHolder().unlockCanvasAndPost(c);
}
}
}
}, stepDelay*i);
}