繁体   English   中英

从Java获取不正确的子图像

[英]Getting incorrect subimage from java

在我的游戏中,我试图制作一个动画的行走序列,所以我试图确保图像将根据其所在的动画帧而变化。 当按下空格键时,图像会发生变化,但是不是从Spritesheet中获取下一张图像,而是获得下一张图像和上一张图像。 我在下面有问题的屏幕截图以及代码。


在按下空格之前

之前
按下空间后

后


private static BufferedImage image;
private static BufferedImage[] cropped = new BufferedImage[15]; 
private static byte frame = 0;

public Player(){
    try {
        image = ImageIO.read(new File("DownWalking.png"));

    } catch (IOException ex) {
        ex.printStackTrace();
    }


}

public void drawPlayer(int x,int y,Graphics2D g2){
    if (frame == 0){
        cropped[0] = image.getSubimage(2, 1, 23, 43);   
    }
    if (frame == 1){
        cropped[1] = image.getSubimage(34, 1, 54, 45);
    }

g2.drawImage(cropped[frame],x * Tile.tileSize, y * Tile.tileSize, cropped[frame].getWidth(), cropped[frame].getHeight(), null);

getSubImage的方法签名为:

getSubimage(int x, int y, int w, int h)

您有getSubimage(x1,y1,x2,y2)导致图像获得2个图块,而不是1个。

资源

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM