How to make that?
Is there a tut about it? if yes please post here the link to it
for (int x = 0; x < frame.width; x += tileWidth) {
for (int y = 0; y < frame.height; y += tileHeight) {
g2d.draw3DRect(x, y, tileWidth, tileHeight, false);
}
}
sloppy sorry, hand written, but that’s how you would do it.
Or, you could do it like the way above, but replace the ‘draw3DRect’ with this below, and it will render tiles with a texture/image.
for (int x = 0; x < frame.width; x += tileWidth) {
for (int y = 0; y < frame.height; y += tileHeight) {
g2d.drawImage(tileImage, x, y, tileWidth, tileHeight, null);
}
}
I added it and i tryed something.
i added a methode that generates a number beetween 1-10. If the number is over 5, then it shows image “Dirt”. Its the number is lower then 5, then it shows “air”. When i run it it generates each time a new map. How to stop it, that it generates at the start ONE time, and after that it doesnt anymore?
Uhh only call it when setting up the game/level?
Idea how to do it?
and yes i tryed mutliple times but it never worked
Well without seeing your code I wouldn’t know, is there anywhere in your game which only gets called once? where you could just call Map.render or whatever?
public boolean loaded = false; // By default set it to false, so it can loop once.
public WorldMap() { // Constructor
load();
}
public void load() {
if (!loaded) {
// Do all the loading here, only once, than set loaded to true.
loaded = true;
} else if (loaded) {
return; // Exit this load() method since all Objects needing loading has already been processed.
}
}
public void update() {
// update game variables
// update graphics/render
// clean up garbage
}
public void run() {
try {
update();
Thread.sleep(400L);
} catch(Exception e) {
e.printStackTrace(System.err);
}
}
Basically create a boolean of ‘loaded’, set it to false by default.
In the constructor of your program/launch point of the class where you’re rendering tiles call the ‘load()’ method.
And by default, if you set ‘loaded = true’ in that method after loading, next time (If called in a game loop/run method), it’s just going to return, it isn’t going to load that method again.
Hope it helps mate, once again sloppy code ;).
You could set up a 2d array when you load up the map. Store the tile’s x, y, and type into it. After you load the map, then you can just render the screen off of the array.