[英]Java: How do you read each line of a text file and setting each line as an array element?
[英]Setting each line in a file to a GlyphLayout in java libgdx
我試圖有5個單獨的GlyphLayout
對象, GlyphLayout
對象在一個文件中包含5行。 我似乎無法正常工作,這是我填充字符串ArrayList
的方式
for(String line : Files.readAllLines(Paths.get("C:\\Users\\Owner\\Desktop\\debugger.txt"))) {
fileElements.add(line);
}
fileElements
是字符串的ArrayList
。 GlyphLayout
對象的另一個ArrayList
稱為glyphs
這是我嘗試呈現它的方式
public void render(SpriteBatch batch) {
elapsedTime += Gdx.graphics.getDeltaTime();
if (elapsedTime > .1f) {
if (stringCounter < fileElements.get(count).length()) {
tmpString += fileElements.get(count).substring(stringCounter, stringCounter + 1);
glyphs.get(count).setText(font, tmpString);
elapsedTime = 0;
stringCounter++;
} else if (count < fileElements.size() - 1) {
count++;
stringCounter = 0;
}
}
for (int i = 0; i < glyphs.size(); i++)
font.draw(batch, glyphs.get(i), position.x, position.y + (10 * i));
}
elapsedTime就是這樣,它一次只打印一個字母,因此看起來很平滑,但是會發生以下情況:
非常感謝您的幫助,我需要在單獨的行中分別包含問題,答案1,答案2等
您可以使用單個BitmapFontCache簡化此過程。 您可以保持整個String完整無缺,並讓字體緩存處理多行的布局。 (如果需要控制垂直間距,則可以使用font.getData().lineHeight = whatever;
)因此,您只需要跟蹤一個BitmapFontCache即可,而無需跟蹤多個GlyphLayouts和Strings。
cache = new BitmapFontCache(font);
要設置要繪制的字符串,請執行此操作。 您需要計算字形的數量以知道要繪制多少個字形(由於空白,字形的數量與String的長度不匹配):
GlyphLayout glyphLayout = cache.setText(yourString, position.x, position.y);
glyphsToDraw = 0;
for (GlyphLayout.GlyphRun run : glyphLayout.runs) glyphsToDraw += run.glyphs.size;
然后渲染非常簡單:
public void render(SpriteBatch batch) {
elapsedTime += Gdx.graphics.getDeltaTime();
cache.draw(batch, 0, Math.min(glyphsToDraw, (int)(elapsedTime * CHARS_PER_SECOND)));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.