TileMap Rendering: Using LibGDX´s TileMap Related Classes
Now that we´ve imported the TileMap related files into our Android module´s Assets folder let´s get into writing the code to get the TileMap rendered on screen.
To Make all this happen LibGDX has a couple of classes that will help us:
TileMap: that will create an object that stores our map information
TmxMapLoader: that will load the data from our .tmx file so it can be stored in the TileMap object
OrthogonalTiledMapRenderer: that will process the information from our TileMap object and renders/draws the map on screen
Now go to your main class (MamBowGame) and declare member variables for the instances of the TileMap and OrthogonalTiledMapRenderer classes
Consequently in the create() method add the following lines of code:
Line 1 creates/initializes a TileMap object and uses LibGDX's TmxMapLoader class to load the information stored in our .tmx file.
Line 2 creates a TiledMapRenderer object passing in the TileMap object which will be the object that later be rendered on screen by calling the tileMapRenderers
If we now add the following code to the render() method just before the sb.begin() call and run our project you will see that the TileMap gets
rendered on screen. The .setView(cam) call ensures that we use the camera's viewport to view the map.
Adding a Background Scroll
Now to have the map scroll as the background for our game add the next 2 lines at the end of the render() method
On line 1 cam.translate(x,y,z); moves the camera every loop through the render method by 10 * delta pixels. Here again we make use
of the deltaTime in our "equation" to make sure the camera moves at the same speed over our map independent of the frame rate. To actually see through the viewport of
the current position of the camera we need to call the OrthograpicCamera's .update() method (line 2).
In the next section we're going to expand a little more on using the camera to create a scrolling background effect and add the for Mam Bow 2's level 1 typical vertical scroll.
If you where wondering what you're MambowGame class should look like right now here it is: