Randomly drawing tiles Libgdx Java -


problem:

i trying create game top down 2d , using pixel tiled map style format, similar top down minecraft. way have few different tiles, different shades of green , brown grass. generate these 4 tiles randomly around 1920*1080 pixel area give semi realistic effect.

ideas:

randomly select tiles, assigning them numerical value, picking random number , using case statement select relevant tile, put them in order in array (not sure how done). render them each using tiled map. 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 of comments rendering errors example removing these comments:

game.batch.begin();         game.batch.draw(currenttile, tilex, tiley);         game.batch.end(); 

gives me error:

tiletry1tiletrytile2tiletryexception in thread "lwjgl application" java.lang.nullpointerexception @ com.badlogic.gdx.graphics.g2d.spritebatch.draw(spritebatch.java:495) @ com.mkgames.optionscreen.generatetile(optionscreen.java:130) @ com.mkgames.optionscreen.<init>(optionscreen.java:84) @ com.mkgames.game1.screen.playoptions.render(playoptions.java:76) @ com.badlogic.gdx.game.render(game.java:46) @ com.mkgames.game1.render(game1.java:39) @ com.badlogic.gdx.backends.lwjgl.lwjglapplication.mainloop(lwjglapplication.java:206) @ com.badlogic.gdx.backends.lwjgl.lwjglapplication$1.run(lwjglapplication.java:114) 

mathutils.random(3) may return 0, according its documentation:

static int random(int range)
returns random number between 0 (inclusive) , specified value (inclusive).

so there 1-in-4 chance currenttile accessed while still null set @ start of while-loop.

add a

case 0: 

to fix this.

a better way of doing create array of tile0 tile3 , use math random value pick array; no need entire repeating case code.


(minor) increasing both x , y with

  tilex+=16;   tiley+=16; 

and so, when working, diagonal line of grass tiles. should set x,y, 0 @ start, increase x. when filled entire horizontal line, reset x 0 , increase y.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -