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)