I’ve created a 21x21 array that I want to use to generate a map with walls(1) and empty space(0). I found a solution on stack overflow and have tried it in Jmonkey, but it doesn’t seem to work. I want the geomWall to be duplicated every time there is a one in the array. Could you help me out with this?
map = new int [20][20];
map[0][0] = 1;
map[19][15] = 1;
Box wall = new Box(1,1,1);
geomWall = new Geometry("Box",wall);
Material wallMat = new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");
wallMat.setColor("Color",ColorRGBA.Cyan);
geomWall.setMaterial(mat);
for (int x = 0; x < map.length; x++) {
for (int y = 0; y < map[x].length; y++) {
if(map[x][y] == 1)
geomWall.clone(true);
geomWall.setLocalTranslation(x, y, 1f);
}
}
rootNode.attachChild(geomWall);
rootNode.attachChild(player);
The wall has been printed at 0,0 but the others won’t appear this is the new code
map = new int [20][20];
map[0][0] = 1;
map[19][15] = 1;
Box wall = new Box(1,1,1);
geomWall = new Geometry("Box",wall);
Geometry geom = geomWall.clone(true);
Material wallMat = new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");
wallMat.setColor("Color",ColorRGBA.Cyan);
geomWall.setMaterial(mat);
for (int x = 0; x < map.length; x++) {
for (int y = 0; y < map[x].length; y++) {
if(map[x][y] == 1)
geom.setLocalTranslation(x, y, 1f);
}
}
Makes a big 2D array for a map.
Makes a box mesh.
Makes a geometry.
Clones that geometry once.
iterates over all of the cells in the map and moves that one cloned geometry.
Clone it where you determine that you need a wall, ie: where you set the translation.
Learning to program the basics while learning to write games can be fun and rewarding but it is also one of the most difficult ways to learn to code. It is sometimes a good idea to take a step back and do some basic coding tutorials until that sort of logical break down really sinks in.
Edit: that being said, a tile based game is one of the better starting points I’ve seen new developers try… so good choice on that.
I tried to declare the geom in the nested for if loop but it throws an exception stating that I can not declare the variable there
for (int x = 0; x < map.length; x++) {
for (int y = 0; y < map[x].length; y++) {
if(map[x][y] == 1)
Geometry geom = geomWall.clone();
geom.setLocalTranslation(x, y, 1f);