Search code examples
javalwjglslick2dtiled

Java - Slick2D TileD Map is Rendering but not Showing


I'm testing the use of TileD with Slick2D in Java, but when I try to import the map and render it, the render function runs smoothly but the map doesn't show up on my screen. My player shows but not the map. I'm new to java so excuse my messy code.

MapRender class:

The render() function is called in a loop after a KeyListener that calls the loadMap() function, and "render" prints to the console but the map never shows. How could I fix this?

package com;

import org.newdawn.slick.SlickException;
import org.newdawn.slick.tiled.TiledMap;

public class MapRender {

    private TiledMap testMap;
    public boolean mapAdded = false;

    public MapRender() {

    }

    public void render() throws SlickException {

        if(mapAdded)
        {
            //Rendering map
            testMap.render(100,100,0,0,100,100);
            System.out.println("render");
        }

    }

    public void loadMap() throws SlickException {

        //Adding map
        testMap = new TiledMap("maps/testMap.tmx", false);
        mapAdded = true;



    }

}

Loop calling render in Main class

public static void main(String[] args) {



    while(true)
    {

        try {
            mapRender.render();
        } catch (SlickException e1) {
            e1.printStackTrace();
        }
    }
}

Any and all help would be appreciated!


Solution

  • The loop you've coded in your main function does not allow Slick2D to run any code of its own in order to update the image on the screen. As a result, your program is probably stuck just rendering the map over and over to some invisible buffer.

    Check out the "Hello World" example on the Slick2D wiki about how to implement the core loop, and add your map rendering call in its render method.