简体   繁体   中英

Randomly drawing tiles Libgdx Java

Problem:

I am trying to create a game that is top down 2d and using a pixel tiled map style format, similar to a top down minecraft. The way i would like to do it is have a few different tiles, different shades of green and brown for grass. I would like to generate these 4 tiles randomly around the 1920*1080 pixel area to give a semi realistic effect.

Ideas:

Randomly select the tiles, assigning them a numerical value, picking a random number and using a case statement to select the relevant tile, then put them in order in an array (not sure how this would be done). Then render them each using Tiled Map. Any ideas???

Have tried this:

private void generateTile(){
    System.out.print("tiletry1");
    while(loadedTiles != 8100){

    System.out.print("tiletry");
    Texture currentTile = null;
    int tileX = 0;
    int tileY = 0;


    switch(MathUtils.random(3)){

    case 1:
        tileX+=16;
        tileY+=16;
        loadedTiles ++;
        //game.batch.draw(tile1, tileX, tileY);
        System.out.print("tile1");
        currentTile = tile1;
        break;
    case 2:
        tileX+=16;
        tileY+=16;
        loadedTiles ++;
        //game.batch.draw(tile2, tileX, tileY);
        System.out.print("tile2");
        currentTile = tile2;
        break;
    case 3:
        tileX+=16;
        tileY+=16;
        loadedTiles ++;
        //game.batch.draw(tile3, tileX, tileY);
        System.out.print("tile3");
        currentTile = tile3;
        break;
        }

    //game.batch.begin();
    //game.batch.draw(currentTile, tileX, tileY);
    //game.batch.end();
    }   

}

But all of the comments to do with rendering get errors for example removing these comments:

game.batch.begin();
        game.batch.draw(currentTile, tileX, tileY);
        game.batch.end();

Gives me this error:

tiletry1tiletrytile2tiletryException in thread "LWJGL Application" java.lang.NullPointerException
at com.badlogic.gdx.graphics.g2d.SpriteBatch.draw(SpriteBatch.java:495)
at com.MKgames.OptionScreen.generateTile(OptionScreen.java:130)
at com.MKgames.OptionScreen.<init>(OptionScreen.java:84)
at com.MKgames.game1.screen.playOptions.render(playOptions.java:76)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.MKgames.Game1.render(Game1.java:39)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)

MathUtils.random(3) may also return 0 , according to its documentation :

static int random(int range)
Returns a random number between 0 (inclusive) and the specified value (inclusive).

So there is a 1-in-4 chance currentTile is accessed while it still is the null to which it is set at the start of the while-loop.

Add a

case 0:

to fix this.

A better way of doing this is to create an array of tile0 to tile3 and simply use the math random value to pick from this array; no need for the entire repeating case code.


(Minor) You are increasing both x and y with

  tileX+=16;
  tileY+=16;

and so, when you get it working, you will get a diagonal line of grass tiles. You should set x,y, to 0 at the start, then only increase x. When you filled an entire horizontal line, reset x to 0 and increase y.

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