在 2d 平台游戏中,玩家有时会被困在方块中

发布于 2024-12-29 01:07:12 字数 3364 浏览 1 评论 0原文

我正在用 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);
    }
}

左右移动的代码:

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);
                }
            }
        }

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

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

I am making a 2d Platformer in Java, and for some reason, when the player jumps towards a platform, it gets stuck sometimes. Here is an image of the problem:

enter image description here

As you see, I was making a jump to the top of the platform, but it got stuck.

Here is my collison code for falling/jumping:

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);
    }
}

Code for moving left and right:

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);
                }
            }
        }

And here is canExecuteMovement function:

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

I really dont know how to tackle this problem, hopefully I can get some enlightenment.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

番薯 2025-01-05 01:07:13

您需要分别跟踪 xy 的允许/拒绝移动代码。发生的情况是,如果用户无法进一步向右移动,代码就会完全阻止他。然而,他应该仍然能够向上或向下移动。

考虑拆分代码中的条件,以便在分配新的移动向量时,它分别设置 xy,检查 中移动的能力xy 分别。

You need to track your allow/deny movement code for x and y separately. What's happening is that if the user can't move any further right the code stops him altogether. However, he should STILL be able to move up or down.

Consider splitting up the conditionals in your code so that when it is assigning the new movement vectors, it sets x and y separately, checking for the ability to move in x and y individually.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文