gpt4 book ai didi

java - 玩家有时会在 2d 平台游戏中卡在一个方 block 中

转载 作者:行者123 更新时间:2023-11-30 06:32:12 27 4
gpt4 key购买 nike

我正在用 Java 制作 2d 平台游戏,出于某种原因,当玩家跳向平台时,它有时会卡住。这是问题的图片:

enter image description here

如您所见,我正在跳到平台顶部,但卡住了。

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

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

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

最佳答案

您需要分别跟踪xy 的允许/拒绝移动代码。发生的事情是,如果用户不能再向右移动,代码将完全阻止他。但是,他应该仍然能够向上或向下移动。

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

关于java - 玩家有时会在 2d 平台游戏中卡在一个方 block 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9011344/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com