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);
}
}
コード左右に移動させる:
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;
}
私は本当にここで
が落下/ジャンプのための私のコリソンコードですこの問題にどのように取り組むべきかわからない、うまくいけば私はいくつかの啓発を得ることができます。
私はcanMoveYとcanMoveXの2つの機能を持っているはずですか?そして、これらの関数はYとXをそれぞれチェックするだけです。 – Qasim
である。あなたのコードは次のようになります: 'if(canMoveY){moveY(); } if(canMoveX){moveX();} } '(しかし、もっとコメントしたいと思いますが) – Tim
私はそれを取ることができますか? – Tim