Libgdx: scrolling background for preview screen

by badasskitteh   Last Updated September 14, 2018 14:13 PM

Good day, I have been coding for quite a while now and I'm at a certain point where I am having difficulties on solving something. I've been creating a preview screen which contains the following:

  1. reads .txt file and stores them in an array which will be displayed on the screen.
  2. the text will have an animation which will be moving upwards like a Star Wars type of intro.
  3. the background of the screen will also move upwards.

The problems that I have encountered are the following:

  • my text did not fit on the screen, but I did try to change the font and the font size and also did change the spacing in the .txt file.
  • the text always starts in the middle of the screen which it should start at the bottom of the screen.
  • the background is buggy, tried to debug it but still can find a solution for a parallaxbackground(scrolling background)

and here is the code.

public class PreviewScreen implements Screen {

    private static DugManMainClass game;
    private SpriteBatch batch;
    private OrthographicCamera camera;
    private Texture tx1 = Assets.bgmenu;
    private BitmapFont font = Assets.menufont2;
    private final static List<String> lines = new ArrayList<String>();
    private static int time = 0;
    private float temp = 0;
    private Sprite s;

    public PreviewScreen(DugManMainClass game) {

        this.game = game;

        //reads .txt file and stores them in an array

        try
        {
            BufferedReader br = new BufferedReader(new InputStreamReader(Gdx.files.internal("previewtext.txt").read()));
            String line = "";
            while ((line = br.readLine()) != null) {
                lines.add(line);
            }
            br.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void render(float delta) {

        float w = s.getHeight();

        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        //temp is a timer which will do the animation effect for the text and background
        temp += Gdx.graphics.getDeltaTime();

        while(temp>1.0f/60.0f)
        {
            PreviewScreen.tick();
            temp -=1.0f/60.0f;

            //here is my problem for the background

            s.setU(temp+1f);
            s.setU2(temp+1f);

        }

        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        s.draw(batch);

// i commented this out because i waited to draw my sprite which is the background text(parallaxbackground)
//      batch.draw(s, 0, -(temp / 10 % w));
//      batch.draw(s, 0, -(temp / 10 % w) + w);

        int yo = time / 4;

        //this is where the the text problem is
        for (int y = 0; y <= 420 / 12; y++) 
        {
            int yl = yo / 12 + 420 / 12 - y;
            if (yl >= 0 && yl < lines.size())
            {
                font.draw(batch,lines.get(yl), (800 - 30 * 12) / 2,  y * 12 + yo % 12);
            }
        }

        batch.end();

    }

    @Override
    public void resize(int width, int height) {}

    @Override
    public void show() {

        batch = new SpriteBatch();

        float w = 800;
        float h = 420;

        camera = new OrthographicCamera();
        camera.setToOrtho(false, w, h);

        tx1.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);

        s = new Sprite(tx1);
        s.setPosition(0, 0);

        Assets.bgsound3.play(1f);

    }

    @Override
    public void dispose() {
        Assets.dispose();
    }

    public static void tick() {
        time++;

        if (Gdx.input.isKeyPressed(Keys.ENTER) || Gdx.input.isTouched()) {
            game.setScreen(new MainMenuScreen(game));
            Assets.bgsound3.stop();
        }

        if (time / 4 > lines.size() * 10 + 250) {

            game.setScreen(new MainMenuScreen(game));
            Assets.bgsound3.stop();
        }
    }

}
Tags : libgdx text


Related Questions


BitmapFont draw nothing on some devices

Updated January 29, 2018 10:13 AM

Scrolling text ticker in libGDX?

Updated September 09, 2018 17:13 PM

Replace text in Unity

Updated April 03, 2016 08:05 AM