简体   繁体   中英

Libgdx Creating fast maps without TiledMap

i am trying to make A game like Minecraft (in 2D). I dont wanna use TiledMap, becouse it cant (ive didnt found a way how) generate random maps.

My Test & code:

public class game implements ApplicationListener {
    private OrthographicCamera camera;
    private SpriteBatch batch;

    private Sprite block[] = new Sprite[26];
    private int createdBlocks = 0;
    private int line = 0;
    @Override
    public void create() {      
        float w = Gdx.graphics.getWidth();
        float h = Gdx.graphics.getHeight();

        camera = new OrthographicCamera(w, h);
        batch = new SpriteBatch();

        camera.position.x = 480;
        camera.position.y = 320;
        camera.update();

        graphics.create();
        player.create();
    }

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

    @Override
    public void render() {
        for(int i=0; i<=25;i++){        
            block[i] = new Sprite(graphics.stone);
            if(i==5 || i==10 || i==15 || i==20)
            line++;
            int i2 = i;
            if(i > 5)
            i = 0;
            block[i].setPosition(i2 * 32, line * 32);
        }
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        player.render(batch);
        for(int i=0; i<=100;i++)
        block[i].draw(batch);
        batch.end();
    }

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

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }
}

its just lagging, slow (loading) and not usefull.

is there a better way?

About your code:

You create lots of new sprites every frame (your loop inside render() method). You should create your sprites in other place, probably in create() method.

About creating map without TiledMap:

You can use SpriteCache instead SpriteBatch for building tile based terrain. You can take a look at example of using SpriteCache here

About random generation of TiledMap

.tmx file - is just XML structured file. You can generate it in code as you want with any XML parser, that's why creating a random TiledMap is not a problem. Here is some basic instructions:

  1. Create tile sets for your random maps.
  2. Create an example map with this sets in tmx editor.
  3. Open it with any text editor and a take a look at file structure.
  4. Generate a similar file in code with any XML parser (you can use libgdx built in parser )

PS I don't recommend you to develop your own solution for 2D maps, in 90% of cases tmx is more than enough for creating 2D maps. You also already have optimized tmx render class inside libgdx.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM