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类中的render方法:

batch.begin();
    for (Rectangle word1 : word.words) {
        font.draw(batch, word.getWordString(), word1.x, word1.y);
    }
batch.end();

它从getWordString()方法中获得正确的值,但它改变了与屏幕上的字体有关的所有内容,而不仅仅是新出现的单词。

编辑:

在尝试执行Sneh的例子后:

 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"。我想我加错东西了

这是因为您正在更新在draw调用中引用的wordString变量。所以当你更新它的值时,它会反映在所有地方。

解决这个问题的方法是将矩形和单词放在一个单独的类中。

public class WordRectangle {
    public String word;
    public Rectangle rectangle;
}

现在在你的word类中有一个以上类的集合,然后在生成一个新单词时向集合中添加新对象。

然后在渲染方法

batch.begin();
    for (WordRectangle word1 : word.words) {
        font.draw(batch, word1.word, word1.rectangle.x, word1.rectangle.y);
    }
batch.end();

我还建议你读一读这篇有用的文章,为你理清思路。

最新更新