Scrolling parallax background, infinitely repeated in libgdx

24,662

Solution 1

Hey I am also making a parrallax background and trying to get it to scroll.

There is a ParallaxTest.java in the repository, it can be found here.

this file is a standalone class, so you will need to incorporate it into your game how you want. and you will need to change the control input since its hooked up to use touch screen/mouse.

this worked for me. as for repeated bg, i havent gotten that far yet, but i think you just need to basic logic as in, ok one screen away from the end, change the first few screens pos to line up at the end.

Solution 2

Beneath where you initialize your Texture for the object. Then beneath that type in this

YourTexture.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);

Where YourTexture is your texture that you want to parallax scroll.

In Your render file type in this code.

batch.draw(YourTexture,0, 0, 0 , srcy, Gdx.graphics.getWidth(),     
           Gdx.graphics.getHeight());  
srcy +=10;

It is going to give you an error so make a variable called srcy. It is nothing too fancy.

Int srcy

Solution 3

You can use setWrap function like below:

Texture texture = new Texture(Gdx.files.internal("images/background.png"));
texture.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);

It will draw background repeatedly! Hope this help!

Share:
24,662
fundead
Author by

fundead

Updated on January 21, 2020

Comments

  • fundead
    fundead over 4 years

    I'm making a 2D sidescrolling space shooter-type game, where I need a background that can be scrolled infintely (it is tiled or wrapped repeatedly). I'd also like to implement parallax scrolling, so perhaps have one lowest background nebula texture that barely moves, a higher one containing far-away stars that barely moves and the highest background containing close stars that moves a lot.

    I see from google that I'd have each layer move 50% less than the layer above it, but how do I implement this in libgdx? I have a Camera that can be zoomed in and out, and in the physical 800x480 screen could show anything from 128x128 pixels (a ship) to a huge area of space featuring the textures wrapped multiple times on their edges.

    How do I continuosly wrap a smaller texture (say 512x512) as if it were infinitely tiled (for when the camera is zoomed right out), and then how do I layer multiple textures like these, keep them together in a suitable structure (is there one in the libgdx api?) and move them as the player's coords change? I've looked at the javadocs and the examples but can't find anything like this problem, apologies if it's obvious!

  • Jente Rosseel
    Jente Rosseel about 11 years
    Can you also set the wrap on textureRegions?
  • RichieHH
    RichieHH over 8 years
    just for reference : this wraps a texture and not a texture region and doesnt work with viewport scaling. It also needs, I believe, a POT texture but may be mistaken.
  • themorfeus
    themorfeus almost 8 years
    It does not repeat infinitely
  • Max
    Max over 7 years