如何渲染不同尺寸的 Tiles?

How do I Render Tiles at different sizes?

所以我有我的瓷砖 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;
    }
    }

这是 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;
}

}

如您所见,我的 DirtTile class 为我的 Tile class 提供了宽度和高度,但是我想尝试制作更宽的瓷砖 (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);

}

}

这样做会将每个 Tile 设置为 128px 宽,我只希望 AnvilTile class 为 128px 宽,所以基本上我必须更改 Tile class 中的渲染方法,但是我不知道该怎么做,任何建议将不胜感激。

这是因为您的 wh 字段是静态的,并且这些值在所有 Tile 实例之间共享。因此,当您创建一个新的 AnvilTile 对象时,对 super 的调用将所有实例中的 wh 字段分别设置为 128 和 64。

删除 static 修饰符以获得您想要的结果。现在字段是实例变量,这意味着每个 Tile 实例都有自己的 wh.