Java 2D 步行动画不工作?

标签 java animation awt java-2d

我目前正在从事一个长期的 2D 游戏项目。截至目前,我遇到了一个非常烦人的错误,每当角色行走时都会创建此错误:http://www.youtube.com/watch?v=39SMSvqQSEc&feature=youtu.be

这是我的 Sprite 表:

Sprite Sheet

这里是渲染方法的代码:

public void render(Screen screen) {
    int xTile = 0;
    int yTile = 28;
    int walkingSpeed = 3;
    int flipTop = (numSteps >> walkingSpeed) & 1;
    int flipBottom = (numSteps >> walkingSpeed) & 1;
    if (movingDir == 1) {
        xTile += 2;
    }
    if (movingDir > 1) {
        flipTop = 0;
        flipBottom = ((numSteps >> 4) & 1);
        if (movingDir == 2) {
            flipTop = 1;
        }
        xTile += 4 + ((numSteps >> 3) & 1) * 2;
    }
    int modifier = scale;
    int xOffset = x - 8;
    int yOffset = y - 11;
    if(isSwimming){
        int waterColor = 0;
        yOffset += 4;
        if(tickCount % 60 < 15){
            yOffset -= 1;
            waterColor = Colors.get(-1, -1, 225, -1);
        }else if(15 <= tickCount % 60 && tickCount % 60 < 30){
            yOffset -= 1;
            waterColor = Colors.get(-1, 225, 115, -1);
        }else if(30 <= tickCount % 60 && tickCount % 60 < 45){
            waterColor = Colors.get(-1, 115, -1, 225);
        }else{
            yOffset -= 1;
            waterColor = Colors.get(-1, 225, 115, -1);
        }
         screen.render(xOffset, yOffset + 3, 0 + 27 * 32, waterColor, 0x00, 1); // Render right half of player/water ripple
        screen.render(xOffset + 8, yOffset + 3, 0 + 27 * 32, waterColor, 0x01, 1); // Render left half of player/water ripple
        }

    screen.render(xOffset + 8 * flipTop, yOffset + 0, xTile + yTile * 32, color, flipTop, scale);
    screen.render(xOffset + 8 - 8 * flipTop, yOffset + 0, xTile + 1 + yTile * 32, color, flipTop, scale);
    if (!isSwimming) {
        screen.render(xOffset + 8 * flipBottom, yOffset + 8, xTile + (yTile + 1) * 32, color, flipBottom, scale);
        screen.render(xOffset + 8 - 8 * flipBottom, yOffset + 8, xTile + 1 + (yTile + 1) * 32, color, flipBottom, scale);
    }

}

和屏幕类:

package ca.fightnight.game.gfx;

public class Screen {

    public static final int MAP_WIDTH = 64;
    public static final int MAP_WIDTH_MASK = MAP_WIDTH - 1;

    public static final byte BIT_MIRROR_X = 0x01;
    public static final byte BIT_MIRROR_Y = 0x01;

    public int[] pixels;

    public int xOffset = 0;
    public int yOffset = 0;

    public int width;
    public int height;

    public SpriteSheet sheet;

    public Screen(int width, int height, SpriteSheet sheet) {
        this.width = width;
        this.height = height;
        this.sheet = sheet;

        pixels = new int[width * height];
    }

    public void render(int xPos, int yPos, int tile, int color, int mirrorDir, int scale) {

        xPos -= xOffset;
        yPos -= yOffset;

        boolean mirrorX = (mirrorDir & BIT_MIRROR_X) > 0;
        boolean mirrorY = (mirrorDir & BIT_MIRROR_Y) > 0;
        int scaleMap = scale - 1;
        int xTile = tile % 32;
        int yTile = tile / 32;
        int tileOffset = (xTile << 3) + (yTile << 3) * sheet.width;
        for (int y = 0; y < 8; y++) {
            int ySheet = y;
            if (mirrorY) 
                ySheet = 7 - y;

            int yPixel = y + yPos + (y * scaleMap) - ((scaleMap << 3) / 2);

            for (int x = 0; x < 8; x++) {
                int xSheet = x;
                if (mirrorX) 
                    xSheet = 7 - x;
                int xPixel = x + xPos + (x * scaleMap) - ((scaleMap << 3) /2 );
                int col = (color >> (sheet.pixels[xSheet + ySheet * sheet.width + tileOffset] * 8)) & 255;
                if (col < 255){ 
                    for(int yScale = 0; yScale < scale; yScale++){
                        if (yPixel + yScale < 0 || yPixel + yScale >= height) 
                            continue;
                        for(int xScale = 0; xScale < scale; xScale++){
                            if (xPixel + xScale < 0 || xPixel + xScale >= width) 
                                continue;
                            pixels[(xPixel + xScale) + (yPixel + yScale) * width] = col;
                        }

                    }

                }
            }

        }

    }

    public void setOffset(int xOffset, int yOffset) {
        this.xOffset = xOffset;
        this.yOffset = yOffset;
    }
}

最佳答案

首先,那个视频让我很开心。其次,为什么不尝试为每个 Sprite 使用整个角色 body ,这样在尝试制作动画时就不会混淆。使用 spritesheet 中的 2 个图像创建行走动画。要向右走,请先让角色面向右,左脚向前,然后右脚向前,然后重复向左走时的过程。

关于Java 2D 步行动画不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18370904/

相关文章:

java - 找不到 persistence.xml

java - 双击鼠标事件后捕获图像

java - Component.getGraphicsConfiguration 线程安全吗?

java - 这是面板的实际尺寸吗?

java - 错误: incompatible types return scores; required: int found: ArrayList<Integer>

java - java库安装的思考

android - 我的 Android Widget 停止刷新 View

javascript - 这是如何制作的(WordBall,透明四面体)

jquery - 如何在做 SCSS 的过程中旋转图像?

java - 循环未从 java 中的 actionPerformed 获取更新的 boolean 值