I am testing creating a game loop without using surface view, but I cannot get the thread to run continuously. In the code below, I am expecting the text view to show the decreasing number continuously, but it only shows it once.
public class ActivityGameTest extends AppCompatActivity {
int bigNumber = Integer.MAX_VALUE;
Thread gameThread;
TextView myText;
private Handler uiHandler;
boolean mRun;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_test);
myText = (TextView) findViewById(R.id.txtHello);
uiHandler = new Handler();
gameThread = new Thread(null, processFrame, "ProcessFrame");
mRun = true;
gameThread.start();
}
private Runnable processFrame = new Runnable() {
@Override
public void run() {
while (mRun) {
updateFrame();
//EDIT - adding these lines
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
private void updateFrame() {
bigNumber--;
uiHandler.post(showFrame);
}
private Runnable showFrame = new Runnable() {
@Override
public void run() {
myText.setText(Integer.toString(bigNumber));
}
};
}