玩家有时会陷入2d平台游戏中

Qas*_*sim 2 java swing physics 2d

我正在用Java创建一个2d平台游戏,出于某种原因,当玩家跳向平台时,它有时会卡住.这是问题的图像:

在此输入图像描述

如你所见,我正在跳到平台的顶部,但它被卡住了.

这是我的坠落/跳跃的碰撞代码:

if (guy.getJumpState() == false) {
    if (canExecuteMovement(0, 8)) {

        ...

        onGround = false;
        if (guy.getY() > this.getParent().getHeight() / 2 - 100) {
            // if you are in the middle, move the platforms.
            for (int i = 0; i < platformCount; i++) {
                if (platform[i].getVisibility() == true) {
                    platform[i].setY(platform[i].getY() - 8);
                }
            }
        } else {
            // or just move the guy if not.
            guy.moveY(8);
        }
    } else {
        onGround = true;
    }
} else {
    if (canExecuteMovement(0, -12)) {

        if (guy.getY() < this.getParent().getHeight() / 2 - 100) {
            // if you are in the middle, move the platforms.
            for (int i = 0; i < platformCount; i++) {
                if (platform[i].getVisibility() == true) {
                    platform[i].setY(platform[i].getY() + 12);
                }
            }
        } else {
            // or just move the guy if not.
            guy.moveY(-12);
        }
        jumpCount++;
        if (jumpCount >= 20) {
            jumpCount = 0;
            guy.setJumpState(false);
        }
    } else {
        jumpCount = 0;
        guy.setJumpState(false);
    }
}
Run Code Online (Sandbox Code Playgroud)

左右移动代码:

if (guy.getDirection() == "left") {
            if (canExecuteMovement(-4, 0)) {
                if (guy.getX() < this.getParent().getWidth() / 2) {
                    // if you are in the middle, move the platforms.
                    for (int i = 0; i < platformCount; i++) {
                        if (platform[i].getVisibility() == true) {
                            platform[i].setX(platform[i].getX() + 4);
                        }
                    }
                } else {
                    // or just move the guy if not.
                    guy.moveX(-4);
                }
            }
        } else if (guy.getDirection() == "right") {
            if (canExecuteMovement(4, 0)) {
                if (guy.getX() > this.getParent().getWidth() / 2) {
                    // if you are in the middle, move the platforms.
                    for (int i = 0; i < platformCount; i++) {
                        if (platform[i].getVisibility() == true) {
                            platform[i].setX(platform[i].getX() - 4);
                        }
                    }
                } else {
                    // or just move the guy if not.
                    guy.moveX(4);
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

这里是canExecuteMovement函数:

private boolean canExecuteMovement(int xChange, int yChange) {
    int projectedX = guy.getX() + xChange;
    int projectedY = guy.getY() + yChange;
    Rectangle projectedBounds = new Rectangle(projectedX, projectedY, guy.getWidth(), guy.getHeight());
    for (int i = 0; i < platformCount; i++) {
        if (projectedBounds.intersects(platform[i].getBounds()) && platform[i].getVisibility() == true) {
            return false;
        }
    }
    return true;
}
Run Code Online (Sandbox Code Playgroud)

我真的不知道如何解决这个问题,希望我能得到一些启示.

Tim*_*Tim 5

你需要跟踪你的允许/拒绝移动代码xy独立.发生的事情是,如果用户不能再向前移动,则代码完全阻止他.但是,他仍然应该能够上下移动.

考虑你的代码分裂了条件语句,这样,当它被赋予新的运动矢量,它集xy分别,检查在中移动的能力xy独立.