so I have my Tile class:
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class Tile {
public static Tile[] tiles = new Tile[256];
public static Tile airTile = new AirTile(0);
public static Tile grassTile = new GrassTile(1);
public static Tile dirtTile = new DirtTile(2);
public static Tile rockTile = new RockTile(3);
//public static Tile anvilTile = new AnvilTile(50);
public static int w, h;
public static final int TILE_WIDTH = 64, TILE_HEIGHT = 64;
private BufferedImage texture;
protected final int id;
public Tile(BufferedImage texture, int id, int w, int h){
this.texture = texture;
this.id = id;
this.h = h;
this.w = w;
tiles[id] = this;
}
public void tick(){
}
public void render(Graphics g, int x, int y){
g.drawImage(texture, x, y, w, h, null);
}
public boolean isSolid(){
return false;
}
public int getId(){
return id;
}
public int getWidth(){
return w;
}
public int getHeight(){
return h;
}
}
and here is an example DirtTile class
import java.awt.image.BufferedImage;
public class DirtTile extends Tile{
public DirtTile(int id) {
super(Assets.dirt, id, 64, 64);
}
@Override
public boolean isSolid(){
return false;
}
}
As you can see my DirtTile class is giving my Tile class it's width and height, however I wanted to try and make a wider tile (128 x 64):
package com.zetcode;
import java.awt.image.BufferedImage;
public class AnvilTile extends Tile{
public AnvilTile(int id) {
super(Assets.anvil, id, 128, 64);
}
}
Doing this sets every Tile to be 128px wide, I only want the AnvilTile class to be 128px wide, so basically i will have to make changes to the Render method in my Tile class, however I don't know how to do this, any suggestions would be greatly appreciated.
It's because your w
and h
fields are static and these values are shared across all Tile
instances. So when you create a new AnvilTile
object, the call to super
sets the w
and h
fields across all instances to 128 and 64 respectively.
Remove the static
modifier to achieve your desired result. Now the fields are instance variables, meaning that each Tile
instance has its own values for w
and h
.