Search code examples
javaandroidlibgdxtiledtmx

Is there a way to make a TiledMap in LibGDX repeat infinitely for an endless runner game?


I used the tiled map editor to make a Tiled map for an android game I'm making in libGDX and I wanted to make it scroll infinitely as the game is an endless runner. However, I have not figured out how to get the map to repeat for an infinite map effect.

I tried to make the character teleport when they reach a certain point but that has a rather jarring effectso I'm looking to see if there is a more elegant way to go about this.


Solution

  • You can use the map renderers with this method. Assuming you run from left to right, then repeat the first part of the map at the end. i.e. adjust your tile map as follows where is what you start with.

    <A Repeated end of map><B First part of map><C Rest of map><D Repeated First part of map>
    

    Then instead of running off the end of the map after C, you run into the repeated section D (which must be large enough to cover the screen). As soon as you get there both character position and map renderer position jump to B and there won't be any change because its an exact copy. is required because otherwise there will be a gap at the left. (Or you could start the character after a screens width of map).