2016-08-25 72 views
0

我尽量让是:LibGDX:是否可以绘制一个BitmapFont并更新它?

  1. 在屏幕的顶部绘制BitmapFont,让它去下到谷底,在那里被删除。
  2. 尽管该BitmapFont仍在继续,但可以用不同的文本绘制另一个BitmapFont。
  3. 重复1和2

这是可以实现的一个BitmapFont或者我必须做出多个BitmapFonts为了这个工作?

编辑:

private BitmapFont font; 

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); 

@Override 
public void render(SpriteBatch batch) { 
batch.setProjectionMatrix(cam.combined); 

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

word.getWordString()是我想说明,这与每一个循环改变文本。它现在所做的是改变最上面生成的新词的文本,也改变上一个词。

EDIT2:

public class Word { 

    public Array<Rectangle> words; 

    public Word(){ 
     words = new Array<Rectangle>(); 
     spawnWord(); 
    } 

    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); 
    } 

    public String getWordString(){ 
     return wordString; 
    } 
} 
+0

你的意思是对象BitmapFont?请张贴一些代码,以便我们可以看到你的BitmapFont是什么。 – IronMonkey

+0

对不起,我添加了一些我认为可以帮助解决的代码。 – GeeSplit

+0

您可以从BitmapFont中创建多个BitmapFontCaches并绘制它们。它们重量更轻,不需要处理。 – Tenfour04

回答

0

您遍历在word.words矩形对象,并正从字1(您从阵列获取的对象)的坐标,但你叫word.getWordString()为他们所有。除非你改变循环内的word.getWordString()的值,否则你得到的字符串将是相同的。这就是为什么你的所有对象都有相同的字符串。

如果你想要在不同的文字上有不同的文字,你需要将它们存储在eatch word1上。

现在看起来您只是使用Rectangle类来跟踪其位置。

如果使用矩形和字符串创建一个名为Word的类,则可以获得所需的结果。

public class Word{ 
    private Rectangle bound; 
    private String wordString; 

    public Word(Rectangle bound, String wordString){ 
     this.bound = bound; 
     this.theWord = wordString; 
    } 

    public String getWordString(){ 
     return wordString; 
    } 

    public Rectangle getBound(){ 
     return bound; 
    } 

    public void updateBound(float x, float y){ 
     bound.x = x; 
     bound.y = y; 
    } 
} 

然后保存您的Word对象在这样对他们的阵列称为单词和循环:

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

EDIT1 我做了一个简单的例子。 pastebin我仍然无法看到你是如何得到你的wordString的,但是这种方式将它存储在每个Word对象上。您不必跟踪字符串并随时更改。你用一个字符串创建一个Word,就是这样。 这也可以从屏幕上删除某个单词或更改特定对象上的文本。

[EDIT2] 我做了一个简单的例子项目: link

+0

我不确定我的理解。我已经有一个Word类,其中包含很多其他的东西。我会尝试过滤出你不需要的东西,并将其放入第二个编辑中。 – GeeSplit

+0

如果有必要,我会给我两个班的完整代码,但我想我应该试着过滤出你不需要的东西。 – GeeSplit

+0

我做了一个快速编辑。如果您需要更多帮助,我需要使用render()和Word类来查看整个班级。 – IronMonkey

相关问题