0
我試圖在libGDX中每毫秒更新標籤一段時間。libGDX標籤在每毫秒更新時有時會停止
但是,有時標籤突然停止沒有錯誤或者我收到一個「字符串索引超出範圍」的錯誤,然後崩潰我的程序。這是兩個不同的問題。
代碼:
Stage stage;
Timer timer = new Timer();
Label countUpLabel;
int countUp;
@Override
public void create() {
stage = new Stage(new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));
//Setting up time label
BitmapFont font = new BitmapFont(Gdx.files.internal("04b_19__-32.fnt"));
LabelStyle labelStyle = new LabelStyle(font, Color.WHITE);
countUpLabel = new Label("Time:\n00000", labelStyle);
countUpLabel.setPosition(200,200);
countUpLabel.setAlignment(Align.center);
countUpLabel.setFontScale(3);
stage.addActor(countUpLabel);
//Setting up timer
timer.schedule(new TimerTask() {
@Override
public void run() {
if (countUp < 3000) {
countUp++;
countUpLabel.setText(String.format("Time:\n%d", countUp));
}else
timer.cancel();
}
}, 0, 1); //every millisecond run the timer
}
在此先感謝。