我试图让多个单词(随机生成)出现在屏幕的顶部并落到底部。这意味着字体在屏幕上被多次绘制。但是每当出现一个新单词时,它应该将该特定单词改为与前一单词不同的内容。这有效,但由于某种原因,它也改变了出现的第一个词。LibGDX:用不同的文字多次绘制一个字体
这里是我的代码:
PlayState类:
public class PlayState extends State {
// TODO: add a background
private BitmapFont font;
private Word word;
public PlayState(GameStateManager gsm) {
super(gsm);
cam.setToOrtho(false, Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2);
// TODO: change the font of the random word
font = new BitmapFont();
font.setColor(Color.BLACK);
word = new Word();
}
@Override
protected void handleInput() {
}
@Override
public void update(float dt) {
handleInput();
}
@Override
public void render(SpriteBatch batch) {
// TODO: make it so it changes the next word that spawns
// Check how much time has passed since a new word has been spawned
// and create a new one if necessary
if (TimeUtils.nanoTime() - word.lastWordTime > 1000000000) {
word.spawnWord();
}
Iterator<Rectangle> iter = word.words.iterator();
while (iter.hasNext()) {
Rectangle word = iter.next();
word.y -= 200 * Gdx.graphics.getDeltaTime(); // move the words at a constant speed of 200 pixels/units per second
// If the word reaches the bottom, remove it from the array
if (word.y + 64 < 0) {
iter.remove();
}
}
batch.setProjectionMatrix(cam.combined);
batch.begin();
for (Rectangle word1 : word.words) {
font.draw(batch, word.getWordString(), word1.x, word1.y);
}
batch.end();
}
@Override
public void dispose() {
font.dispose();
}
}
字类:
public class Word {
public Array<Rectangle> words;
public long lastWordTime;
private FileHandle file, file2;
private BufferedReader reader, reader2;
private List<String> lines, lines2;
private String line, line2;
private Random random;
private String wordString;
public Word(){
words = new Array<Rectangle>();
spawnWord();
}
// Set the new Rectangle to a random position at the top of the screen
// and adds it to the words array
public void spawnWord() {
Rectangle word = new Rectangle();
word.x = MathUtils.random(0, Gdx.graphics.getWidth()/2 - 64);
word.y = Gdx.graphics.getHeight();
words.add(word);
lastWordTime = TimeUtils.nanoTime();
// Read the file and put it into a list of strings
file = Gdx.files.internal("words/wordsEn.txt");
file2 = Gdx.files.internal("words/swearWords.txt");
reader = new BufferedReader(file.reader());
reader2 = new BufferedReader(file2.reader());
lines = new ArrayList<String>();
lines2 = new ArrayList<String>();
try {
line = reader.readLine();
line2 = reader2.readLine();
} catch (IOException e) {
e.printStackTrace();
}
while (line != null && line2 != null){
lines.add(line);
lines2.add(line2);
try {
line = reader.readLine();
line2 = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
// Choose a random string from the list
random = new Random();
wordString = lines.get(random.nextInt(lines.size()));
// Filter out bad words
if (lines2.contains(wordString)) {
wordString = lines.get(random.nextInt(lines.size()));
}
}
public String getWordString(){
return wordString;
}
}
我想我把范围缩小到渲染方法在PlayState类:
batch.begin();
for (Rectangle word1 : word.words) {
font.draw(batch, word.getWordString(), word1.x, word1.y);
}
batch.end();
它从getWordString()方法获取正确的值,但它改变了与屏幕上的字体相关的所有内容,而不是仅显示新出现的单词。
编辑:
试图实现斯内德的例子后:
public void spawnWord() {
wordRectangle.word.x = MathUtils.random(0, Gdx.graphics.getWidth()/2 - 64);
wordRectangle.word.y = Gdx.graphics.getHeight();
words.add(wordRectangle.word);
lastWordTime = TimeUtils.nanoTime();
我正在上words.add(wordRectangle.word)
一个错误,指出“矩形不能被转换为WordRectangle”。我想我正在添加错误的东西。
word.getWordString()是对wordString的引用。当你在一个地方改变wordString时,它反映在其他地方。 – Sneh