【问题标题】:LibGDX: Draw one font multiple times with different textLibGDX:用不同的文本多次绘制一种字体
【发布时间】:2016-12-31 19:43:03
【问题描述】:

我正在尝试让多个单词(随机生成)出现在屏幕顶部并落到底部。这意味着字体在屏幕上被多次绘制。但是每当出现一个新词时,它应该将该特定词更改为与前一个词不同的东西。这行得通,但由于某种原因,它也改变了出现的第一个单词。

这是我的代码:

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() 获得正确的值,但它改变了与屏幕上的字体有关的所有内容,而不仅仅是新出现的单词。

编辑:

尝试实现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”。我想我添加了错误的东西。

【问题讨论】:

  • word.getWordString() 是对 wordString 的引用。当您在一个地方更改 wordString 时,它会反映在其他所有地方。

标签: java android android-studio libgdx


【解决方案1】:

这是因为您正在更新您的绘图调用中引用的 wordString 变量。因此,当您更新它的价值时,它会随处体现。

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

public class WordRectangle {

    public String word;

    public Rectangle rectangle;

}

现在在您的单词类中有一个上述类的集合,然后在您生成新单词时将新对象添加到集合中。

然后在你的渲染方法中

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

我还建议您阅读 this 有用的文章来为您清除问题。

【讨论】:

  • 我阅读了这篇文章并尝试了您的解决方案,但我收到了渲染方法的错误,即 WordRectangle 应该是 Rectangle。我不完全明白发生了什么。
  • 是的,因为您可能正在尝试将矩形存储到单词矩形中。正如我所提到的,你将不得不使用 Array 然后在里面添加你的矩形和单词以及 WordRectangle 的对象,然后最后添加到集合中。
  • 我故意留下了那部分,以便您自己理解和尝试。
  • 我完全迷路了。我创建了 WordRectangle 对象并将其添加到数组中,但它再次给了我一个错误。我很感激你没有立即给出答案,我更喜欢这样! :D
  • 我在原始帖子中进行了编辑。它基本上说“矩形不能转换为WordRectangle”,我认为这与我添加错误的东西有关。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-20
  • 1970-01-01
  • 2017-09-18
  • 2017-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多