1

What I try to make is:

  1. Draw a BitmapFont on the top of the screen and let it go down to the bottom, where it gets deleted.
  2. While that BitmapFont is still making its way down, draw another BitmapFont with different text.
  3. Repeat 1 and 2.

Is this achievable with one BitmapFont or do I have to make multiple BitmapFonts in order for this to work?

EDIT:

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() is the text I want to show, which changes with every loop. What it does now is change the text of the newly drawn word that spawns at the top, but also the previous one.

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;
    }
}
GeeSplit
  • 53
  • 3
  • 12
  • Do you mean the object BitmapFont? Please post some code so we can see what your BitmapFont is. – Jim Aug 25 '16 at 11:06
  • Sorry, added a bit of my code that I think could help solve it. – GeeSplit Aug 25 '16 at 11:20
  • You can create multiple BitmapFontCaches out of your BitmapFont and draw those. They are much lighter weight and don't have to be disposed. – Tenfour04 Aug 25 '16 at 18:18

1 Answers1

0

You are looping over the Rectangle objects in word.words and getting the coordinates from word1 (the object you get from the array) but you call word.getWordString() for them all. The unless you change the value of word.getWordString() inside the loop the string you get will be the same. That is why you have the same string for all your objects.

If you want to have different text on eatch word1 object you need to store them on eatch word1.

Right now it looks like you are just using the Rectangle class to keep track of its position.

If you make a class called Word with a Rectangle and a String on it you can get the result you want.

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

Then store your Word objects in an array called words and loop over them like this:

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

EDIT1 I made a quick example. pastebin I still can't see how you get your wordString, but this way you store it on every Word object. You don't have to keep track of a String and changing it all the time. You create a Word with a string and thats it. This also make it possible to remove a certain word from the screen or change the text on a specific object.

[EDIT2] I made a quick example project: link

Jim
  • 995
  • 2
  • 11
  • 28
  • I'm not sure I understand. I already have a Word class though, which contains a lot of other stuff. I'll try to filter out the things you don't need and put it in the second EDIT. – GeeSplit Aug 25 '16 at 14:39
  • If necessary I'll give the full code of my two classes, but I figured I should try and filter out the stuff you won't need. – GeeSplit Aug 25 '16 at 14:44
  • I made a quick edit. If you need more help I need to see the entire class with you render() and the Word class. – Jim Aug 25 '16 at 19:08
  • So you are doing a lot of things I don't like. Like reading strings from the same files every time you spawn a Word. I think you need to re-think the whole thing. I am at work right now but I am going to work on my own game project later today and I kan make you a working example of how you could do it in a lot simpler way that would also be much more flexible. – Jim Aug 26 '16 at 08:33
  • It's basically code from 3 different tutorials on different stuff that I tried to implement into my code. This went horribly wrong though. Thanks for taking the time to help me! – GeeSplit Aug 26 '16 at 10:09
  • I made the example for you. The link is in the answer. – Jim Aug 26 '16 at 18:06
  • The dictionary class gives me an error "... is not supported at this language level". This happens when at the readFiles method. – GeeSplit Aug 26 '16 at 20:32
  • It uses some lamda expressions. You need to set it to use jdk 1.8. File -> Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler Then set your modules to use 1.8 (Target bytecode version) – Jim Aug 26 '16 at 20:43
  • Still gives me the error. I didn't find the option at first though, but after some googling I managed to find it under File -> Other Settings -> Default Settings – GeeSplit Aug 27 '16 at 10:23
  • Try File -> Project Structure and under Project set Project SDK to 1.8 – Jim Aug 27 '16 at 11:06
  • This solution didn't work either. However, after some more googling (should have done that earlier, laziness on my end) I found [this](http://stackoverflow.com/questions/22703412/java-lambda-expressions-not-supported-at-this-language-level) thread. The answer there worked for me. I will implement this code and try it out later today or tomorrow, don't have time right now. – GeeSplit Aug 27 '16 at 15:03
  • Is it possible to change that Lamda expression? The android device I'm testing with has API 19, but the min. API needed for `stream.forEach` is 24. – GeeSplit Aug 28 '16 at 15:38