首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BufferedImage到BufferedImage数组,Tiles Disapear

BufferedImage到BufferedImage数组,Tiles Disapear
EN

Stack Overflow用户
提问于 2015-12-02 03:48:20
回答 1查看 236关注 0票数 0

好的,斯塔克溢出,你是我现在的最后一行。

如果您查看下面的代码和图片,您将注意到有两个文件被迅速命名。

Tile.java

TileMap.java

有关这些类的更多信息,请参见该项目的下载文件"ForeignGuyMike龙故事教程第8部分“。到目前为止,我正在上他的课阅读瓷砖,因为它似乎相当有效的我的努力。切换到BufferedImage数组的原因是为了允许每个块都有动画,因为现在它确实可以处理包含超过一个帧的图像。

现在,对于这两个类:它们是故意设置的,不通过图像动画,只是为了显示他们打破了这个程序,没有结合我的成像动画功能。

它们在这里

在代码/图像中断之前

代码语言:javascript
复制
public class TileMap {

// position
private double x;
private double y;

// bounds
private int xmin;
private int ymin;
private int xmax;
private int ymax;

private double tween;

// map
private int[][] map;
private int tileSize;
private int numRows;
private int numCols;
private int width;
private int height;

// tileset
private BufferedImage[] tileset;
private int numTilesAcross;
private Tile[][] tiles;

private Animation an;

// drawing
private int rowOffset;
private int colOffset;
private int numRowsToDraw;
private int numColsToDraw;

public TileMap(int tileSize) {
    this.tileSize = tileSize;
    numRowsToDraw = GamePanel.HEIGHT / tileSize + 2;
    numColsToDraw = GamePanel.WIDTH / tileSize + 2;
    tween = 0.07;
    an = new Animation();
}

public void loadTiles(BufferedImage[] s, int delay) {

    an.setDelay(delay);
    an.setFrames(s);

    try {

        tileset = s;

        numTilesAcross = tileset[0].getWidth() / tileSize;
        tiles = new Tile[2][numTilesAcross];

        BufferedImage[] subimage = new BufferedImage[s.length];
        for(int col = 0; col < numTilesAcross; col++) {
            subimage[0] = tileset[0].getSubimage(
                        col * tileSize,
                        0,
                        tileSize,
                        tileSize
                    );
            tiles[0][col] = new Tile(subimage[0], Tile.NORMAL);
            subimage[0] = tileset[0].getSubimage(
                        col * tileSize,
                        tileSize,
                        tileSize,
                        tileSize
                    );
            tiles[1][col] = new Tile(subimage[0], Tile.BLOCKED);
        }

    }
    catch(Exception e) {
        e.printStackTrace();
    }

}

public void loadMap(String s) {

    try {

        InputStream in = getClass().getResourceAsStream(s);
        BufferedReader br = new BufferedReader(
                    new InputStreamReader(in)
                );

        numCols = Integer.parseInt(br.readLine());
        numRows = Integer.parseInt(br.readLine());
        map = new int[numRows][numCols];
        width = numCols * tileSize;
        height = numRows * tileSize;

        xmin = GamePanel.WIDTH - width;
        xmax = 0;
        ymin = GamePanel.HEIGHT - height;
        ymax = 0;

        String delims = "\\s+";
        for(int row = 0; row < numRows; row++) {
            String line = br.readLine();
            String[] tokens = line.split(delims);
            for(int col = 0; col < numCols; col++) {
                map[row][col] = Integer.parseInt(tokens[col]);
            }
        }

    }
    catch(Exception e) {
        e.printStackTrace();
    }

}

public int getTileSize() { return tileSize; }
public double getx() { return x; }
public double gety() { return y; }
public int getWidth() { return width; }
public int getHeight() { return height; }

public int getType(int row, int col) {
    int rc = map[row][col];
    int r = rc / numTilesAcross;
    int c = rc % numTilesAcross;
    return tiles[r][c].getType();
}

public void setTween(double d) { tween = d; }

public void setPosition(double x, double y) {

    this.x += (x - this.x) * tween;
    this.y += (y - this.y) * tween;

    fixBounds();

    colOffset = (int)-this.x / tileSize;
    rowOffset = (int)-this.y / tileSize;

}

private void fixBounds() {
    if(x < xmin) x = xmin;
    if(y < ymin) y = ymin;
    if(x > xmax) x = xmax;
    if(y > ymax) y = ymax;
}

public void update() {
    an.update();
}

public void draw(Graphics2D g) {

    for(
        int row = rowOffset;
        row < rowOffset + numRowsToDraw;
        row++) {

        if(row >= numRows) break;

        for(
            int col = colOffset;
            col < colOffset + numColsToDraw;
            col++) {

            if(col >= numCols) break;

            if(map[row][col] == 0) continue;

            int rc = map[row][col];
            int r = rc / numTilesAcross;
            int c = rc % numTilesAcross;

            g.drawImage(
                tiles[r][c].getImage(),
                (int)x + col * tileSize,
                (int)y + row * tileSize,
                null
            );

        }

    }

}

public class Tile {

private BufferedImage image;
private int type;

// tile types
public static final int NORMAL = 0;
public static final int BLOCKED = 1;

public Tile(BufferedImage image, int type) {
    this.image = image;
    this.type = type;
}

public BufferedImage getImage() { return image; }
public int getType() { return type; }

下面是实现更改后的代码

变更组:

Tile.java

-Constructor Param's已经从BufferedImage变成BufferedImage[]

-BufferedImage图像到BufferedImage[]图像;

-getImage to getImage(int i) {返回imagei;}

TileMap.java

-Initialized子图像并将其设置为BufferedImages.length;

-Removed所有子图像到子图像

绘图中的-getImage现在是getImage(0);//硬编码它

之后

代码语言:javascript
复制
public class TileMap {

// position
private double x;
private double y;

// bounds
private int xmin;
private int ymin;
private int xmax;
private int ymax;

private double tween;

// map
private int[][] map;
private int tileSize;
private int numRows;
private int numCols;
private int width;
private int height;

// tileset
private BufferedImage[] tileset;
private int numTilesAcross;
private Tile[][] tiles;

private Animation an;

// drawing
private int rowOffset;
private int colOffset;
private int numRowsToDraw;
private int numColsToDraw;

public TileMap(int tileSize) {
    this.tileSize = tileSize;
    numRowsToDraw = GamePanel.HEIGHT / tileSize + 2;
    numColsToDraw = GamePanel.WIDTH / tileSize + 2;
    tween = 0.07;
    an = new Animation();
}

public void loadTiles(BufferedImage[] s, int delay) {

    an.setDelay(delay);
    an.setFrames(s);

    try {

        tileset = s;

        numTilesAcross = tileset[0].getWidth() / tileSize;
        tiles = new Tile[2][numTilesAcross];

        BufferedImage[] subimage = new BufferedImage[s.length];
        for(int col = 0; col < numTilesAcross; col++) {
            subimage[0] = tileset[0].getSubimage(
                        col * tileSize,
                        0,
                        tileSize,
                        tileSize
                    );
            tiles[0][col] = new Tile(subimage, Tile.NORMAL);
            subimage[0] = tileset[0].getSubimage(
                        col * tileSize,
                        tileSize,
                        tileSize,
                        tileSize
                    );
            tiles[1][col] = new Tile(subimage, Tile.BLOCKED);
        }

    }
    catch(Exception e) {
        e.printStackTrace();
    }

}

public void loadMap(String s) {

    try {

        InputStream in = getClass().getResourceAsStream(s);
        BufferedReader br = new BufferedReader(
                    new InputStreamReader(in)
                );

        numCols = Integer.parseInt(br.readLine());
        numRows = Integer.parseInt(br.readLine());
        map = new int[numRows][numCols];
        width = numCols * tileSize;
        height = numRows * tileSize;

        xmin = GamePanel.WIDTH - width;
        xmax = 0;
        ymin = GamePanel.HEIGHT - height;
        ymax = 0;

        String delims = "\\s+";
        for(int row = 0; row < numRows; row++) {
            String line = br.readLine();
            String[] tokens = line.split(delims);
            for(int col = 0; col < numCols; col++) {
                map[row][col] = Integer.parseInt(tokens[col]);
            }
        }

    }
    catch(Exception e) {
        e.printStackTrace();
    }

}

public int getTileSize() { return tileSize; }
public double getx() { return x; }
public double gety() { return y; }
public int getWidth() { return width; }
public int getHeight() { return height; }

public int getType(int row, int col) {
    int rc = map[row][col];
    int r = rc / numTilesAcross;
    int c = rc % numTilesAcross;
    return tiles[r][c].getType();
}

public void setTween(double d) { tween = d; }

public void setPosition(double x, double y) {

    this.x += (x - this.x) * tween;
    this.y += (y - this.y) * tween;

    fixBounds();

    colOffset = (int)-this.x / tileSize;
    rowOffset = (int)-this.y / tileSize;

}

private void fixBounds() {
    if(x < xmin) x = xmin;
    if(y < ymin) y = ymin;
    if(x > xmax) x = xmax;
    if(y > ymax) y = ymax;
}

public void update() {
    an.update();
}

public void draw(Graphics2D g) {

    for(
        int row = rowOffset;
        row < rowOffset + numRowsToDraw;
        row++) {

        if(row >= numRows) break;

        for(
            int col = colOffset;
            col < colOffset + numColsToDraw;
            col++) {

            if(col >= numCols) break;

            if(map[row][col] == 0) continue;

            int rc = map[row][col];
            int r = rc / numTilesAcross;
            int c = rc % numTilesAcross;

            g.drawImage(
                tiles[r][c].getImage(0), // hard code the image at 0
                (int)x + col * tileSize,
                (int)y + row * tileSize,
                null
            );

        }

    }

}

public class Tile {

private BufferedImage[] image;
private int type;

// tile types
public static final int NORMAL = 0;
public static final int BLOCKED = 1;

public Tile(BufferedImage[] image, int type) {
    this.image = image;
    this.type = type;
}

public BufferedImage getImage(int i) { return image[i]; }
public int getType() { return type; }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-02 12:49:14

对于任何想知道答案的人来说,这个问题已经解决了。

节段

代码语言:javascript
复制
BufferedImage[] subimage = new BufferedImage[s.length];
    for(int col = 0; col < numTilesAcross; col++) {
        subimage[0] = tileset[0].getSubimage(
                    col * tileSize,
                    0,
                    tileSize,
                    tileSize
                );
        tiles[0][col] = new Tile(subimage, Tile.NORMAL);
        subimage[0] = tileset[0].getSubimage(
                    col * tileSize,
                    tileSize,
                    tileSize,
                    tileSize
                );
        tiles[1][col] = new Tile(subimage, Tile.BLOCKED);
    }

是罪魁祸首感谢haraldk为测试此问题提供的建议,修复方法是将子映像简单地放在循环中。不断地更改内容并使用相同的数组只是在整个数组中存储其最后一个内容,从而造成混乱。

更新代码示例

代码语言:javascript
复制
    for(int col = 0; col < numTilesAcross; col++) {
        BufferedImage[] subimage = new BufferedImage[s.length];
        subimage[0] = tileset[0].getSubimage(
                    col * tileSize,
                    0,
                    tileSize,
                    tileSize
                );
        tiles[0][col] = new Tile(subimage, Tile.NORMAL);
        subimage[0] = tileset[0].getSubimage(
                    col * tileSize,
                    tileSize,
                    tileSize,
                    tileSize
                );
        tiles[1][col] = new Tile(subimage, Tile.BLOCKED);
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34034572

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档