2016-08-24 29 views
0

我试图让多个单词(随机生成)出现在屏幕的顶部并落到底部。这意味着字体在屏幕上被多次绘制。但是每当出现一个新单词时,它应该将该特定单词改为与前一单词不同的内容。这有效,但由于某种原因,它也改变了出现的第一个词。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”。我想我正在添加错误的东西。

+1

word.getWordString()是对wordString的引用。当你在一个地方改变wordString时,它反映在其他地方。 – Sneh

回答

0

这是因为您正在更新您的绘图调用中引用的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有用的文章,以清楚的事情我们为您服务。

+0

我阅读了文章并试用了你的解决方案,但是我发现渲染方法出现错误,说'WordRectangle'应该是'Rectangle'。我不完全理解发生了什么。 – GeeSplit

+0

是的,因为您可能试图将矩形存储到字矩形中。正如我所提到的,你将不得不使用Array ,然后将你的矩形和单词添加到WordRectangle的对象中,然后添加到集合中。 – Sneh

+0

我故意留下了这部分,以便您可以自己理解并尝试。 – Sneh

相关问题