Hello,i just recently started programming in java.I watched all of TheNewBoston’s beginners tutorials and then hopped right away in to game development.I found some nice tutorials on how to make a small Minecraft 2D Creative version,i got to the part where the guy coded the block placing event i did everything exactly like he did (or at least i think that i did it the exact same way) but it does not work for me ;/.
public class Level {
public int worldW = 50,worldH=50;
public Block[][] block = new Block[worldW][worldH];
public Level(){
for(int x=0;x<block.length;x++){
for(int y=0;y<block[0].length;y++){
block[x][y] = new Block(new Rectangle(x * Tile.tileSize, y * Tile.tileSize, Tile.tileSize, Tile.tileSize), Tile.air);
}
}
generateLevel();
}
public void generateLevel(){
//generating mountains,earth etc.
for(int y=0;y<block.length;y++){
for(int x=0;x<block[0].length;x++){
if (y>worldH/4){
if(new Random().nextInt(100)>20){
try{
if(block[x-1][y-1].id== Tile.earth){
block[x][y].id = Tile.earth;
}
}catch(Exception e){}
}
if(new Random().nextInt(100)>30){
try{
if(block[x+1][y-1].id== Tile.earth){
block[x][y].id = Tile.earth;
}
}catch(Exception e){}
}
try{
if(block[x][y-1].id== Tile.earth){
block[x][y].id = Tile.earth;
}
}catch(Exception e){}
if(new Random().nextInt(100)<5){
block[x][y].id = Tile.earth;
}
}
}
}
//placing grass
for(int y=0;y<block.length;y++){
for(int x=0;x<block[0].length;x++){
if(block[x][y].id == Tile.earth && block[x][y-1].id == Tile.air){
block[x][y].id = Tile.grass;
}
}
}
}
public void building(int camX,int camY,int renW,int renH){
if(Component.isMouseLeft || Component.isMouseRight){
for(int x=(camX/Tile.tileSize);x<(camX/Tile.tileSize) +renW;x++){
for(int y=(camY/Tile.tileSize);y<(camY/Tile.tileSize)+renH ;y++){
if(x >= 0 && y >= 0 && x < worldW && y< worldH){
if(block[x][y].contains(new Point((Component.mse.x /Component.pixelSize)+(int)Component.sX,(Component.mse.y /Component.pixelSize)+ (int)Component.sY))){
int sid[] = Component.inventory.invBar[Inventory.selected].id;
if(Component.isMouseLeft){
block[x][y].id = Tile.air;
}else if (Component.isMouseRight){
if(sid == Tile.earth||sid == Tile.grass||sid == Tile.sand){
block[x][y].id = sid;
}
}
break;
}
}
}
}
}
}
public void tick(int camX,int camY,int renW,int renH){
building(camX,camY,renW,renH);
}
public void render(Graphics g,int camX,int camY,int renW,int renH){
for(int x=(camX/Tile.tileSize);x<(camX/Tile.tileSize) +renW;x++){
for(int y=(camY/Tile.tileSize);y<(camY/Tile.tileSize)+renH ;y++){
if(x >= 0 && y >= 0 && x < worldW && y< worldH){
block[x][y].render(g);
}
}
}
}
}
He codes the block placing at around 1h : 38min
http://www.youtube.com/watch?v=JHxtxfUU5Is Does anyone have an idea why mine doesnt work ?