当发现侧面碰撞时重力停止

请看这个GIF:

在这里输入图像说明

animation上的标签显示“移动button被按下,然后释放”。 而且你可以看到什么时候被按下(和玩家的getCenterY()在墙上getCenterY()),重力不起作用。 我试图从昨天开始解决,但是我不能。 所有方法都是从游戏循环中调用的。

public void move() { if (left) { switch (game.currentLevel()) { case 1: for (int i = 0; i < game.lvl1.getX().length; i++) game.lvl1.getX()[i] += game.physic.xVel; break; } } else if (right) { switch (game.currentLevel()) { case 1: for (int i = 0; i < game.lvl1.getX().length; i++) game.lvl1.getX()[i] -= game.physic.xVel; break; } } } int manCenterX, manCenterY, boxCenterX, boxCenterY; //gravity stop public void checkCollision() { for (int i = 0; i < game.lvl1.getX().length; i++) { manCenterX = (int) game.man.getBounds().getCenterX(); manCenterY = (int) game.man.getBounds().getCenterY(); if (game.man.getBounds().intersects(game.lvl1.getBounds(i))) { boxCenterX = (int) game.lvl1.getBounds(i).getCenterX(); boxCenterY = (int) game.lvl1.getBounds(i).getCenterY(); if (manCenterY - boxCenterY > 0 || manCenterY - boxCenterY < 0) { game.man.setyPos(-2f); game.man.isFalling = false; } } } } //left side of walls public void colliLeft() { for (int i = 0; i < game.lvl1.getX().length; i++) { if (game.man.getBounds().intersects(game.lvl1.getBounds(i))) { if (manCenterX - boxCenterX < 0) { for (int i1 = 0; i1 < game.lvl1.getX().length; i1++) { game.lvl1.getX()[i1] += game.physic.xVel; game.man.isFalling = true; } } } } } //right side of walls public void colliRight() { for (int i = 0; i < game.lvl1.getX().length; i++) { if (game.man.getBounds().intersects(game.lvl1.getBounds(i))) { if (manCenterX - boxCenterX > 0) { for (int i1 = 0; i1 < game.lvl1.getX().length; i1++) { game.lvl1.getX()[i1] += -game.physic.xVel; game.man.isFalling = true; } } } } } public void gravity() { game.man.setyPos(yVel); } //not called from gameloop: public void setyPos(float yPos) { this.yPos += yPos; } 

如果他在块的顶部,更改删除掉的代码:

 if (manCenterY - boxCenterY > 0 || manCenterY - boxCenterY < 0) { game.man.setyPos(-2f); game.man.isFalling = false; } 

如果代码位于块的顶部,则代码将消除掉落:

 if (manAboveBlock && (manCenterY - boxCenterY > 0 || manCenterY - boxCenterY < 0)) { game.man.setyPos(-2f); game.man.isFalling = false; } 

这是治疗症状而不是问题。 问题可能是当右边的墙被按下时,玩家在墙内移动一会儿,然后在墙外移动。 当玩家被剪切到墙上时,他的isFalling值被设置为false。