Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 2D平台游戏中的碰撞错误_Java_2d_Game Engine_Collision - Fatal编程技术网

Java 2D平台游戏中的碰撞错误

Java 2D平台游戏中的碰撞错误,java,2d,game-engine,collision,Java,2d,Game Engine,Collision,我是java和游戏编程新手,我正在开始我的第一个大项目,这是一个2D平台益智游戏。 这是我的玩家移动代码 if (speedX > 0 && centerX <= 400){ centerX += speedX; } if (speedX < 0 && centerX >= 400){ centerX += speedX; } if (speedX > 0 && center

我是java和游戏编程新手,我正在开始我的第一个大项目,这是一个2D平台益智游戏。 这是我的玩家移动代码

  if (speedX > 0 && centerX <= 400){
     centerX += speedX;
  }

  if (speedX < 0 && centerX >= 400){
     centerX += speedX;
  }

  if (speedX > 0 && centerX >= 400){
     bg1.setSpeedX(-MOVESPEED);
     bg2.setSpeedX(-MOVESPEED);
  }

  if (speedX < 0 && centerX <= 400){
     bg1.setSpeedX(MOVESPEED);
     bg2.setSpeedX(MOVESPEED);
  }

  if (speedX == 0){
     bg1.setSpeedX(0);
     bg2.setSpeedX(0);
  }

  if(movingRight == true && movingLeft == true ){
     bg1.setSpeedX(0);
     bg2.setSpeedX(0);
  }

  // Handles Jumping
  if (jumped == true) {
     speedY += 1;


  }

  // Prevents going beyond X coordinate of 0
  if (centerX + speedX <= 60) {
     centerX = 61;
  }
  rect.setRect(centerX - 47, centerY - 65, 32, 87);

  centerY += speedY;
 }

 public void moveRight() {
        speedX = MOVESPEED;      

 }

 public void moveLeft() {
       speedX = -MOVESPEED;
 }

 public void stopRight() {
    movingRight = false;
    stop();
   }

 public void stopLeft() {
   movingLeft = false;
    stop();
 }

 private void stop() {
  if (movingRight == false && movingLeft == false) {
     speedX = 0;
  }
  if (movingRight == false && movingLeft == true) {
     moveLeft();
  }

  if (movingRight == true && movingLeft == false) {
     moveRight();
  }
}



 public void jump() {
  if (jumped == false) {
     speedY = JUMPSPEED;
     jumped = true;
  }

}
有两个问题。第一个问题是,如果我在让角色向右或向左移动时按下其中一个移动键。 我知道会发生这种情况,因为我对它进行了编程,如果角色在向右或向左移动时与地面相交,那么他会向右或向左移动。我这样做是为了让水平碰撞起作用,我想不出任何其他方法来做这件事或如何修复它

第二个问题是,如果角色从平台上移动,他不会摔倒。 我试图通过添加碰撞方法来修复它

else{
speedY += 1;
}
但出于某种原因,它让这个角色消失了


非常感谢
public void checkCollision(Rectangle rect){
    if(player.intersects(rect)) {
        //the rectangles intersect, time to move the player out of the block
        if(rect.y+rect.height >= player.y && rect.y+rect.height-0.7f < player.y) { //if the player is at most 0.7 units (you should change this!) below top side
            player.y = rect.y+rect.height; //set player to stand on top
            speed.y = 0f; //stop the movement
            onGround = true;
        } else if(rect.y+rect.height > player.y && rect.y < player.y+player.height) { //if the playeer is on the side, but not below
            float xEscMinus = (float)Math.abs((rect.x+rect.width)-player.x); //find the distance to the side
            float xEscPlus  = (float)Math.abs(rect.x-(player.x+player.width));

            if(xEscMinus<xEscPlus) {
                player.x = rect.x+rect.width;
            } else {
                player.x = rect.x-player.width;
            }
        }
    }
}

为什么仅当角色在checkCollision方法中向上移动时才检查垂直碰撞?我不认为速度很重要,检查应该总是发生。我在检查角色是向右、向左还是向下移动时的碰撞。它与速度无关,我只是不知道如何正确处理碰撞。你的代码不起作用,因为如果你向左或向右移动,你的垂直速度是0,所以检查不会发生。如果玩家与矩形相交,你必须通过最近的边将他移出矩形。我将用我的一个旧项目的代码发布一个答案。
public void checkCollision(Rectangle rect){
    if(player.intersects(rect)) {
        //the rectangles intersect, time to move the player out of the block
        if(rect.y+rect.height >= player.y && rect.y+rect.height-0.7f < player.y) { //if the player is at most 0.7 units (you should change this!) below top side
            player.y = rect.y+rect.height; //set player to stand on top
            speed.y = 0f; //stop the movement
            onGround = true;
        } else if(rect.y+rect.height > player.y && rect.y < player.y+player.height) { //if the playeer is on the side, but not below
            float xEscMinus = (float)Math.abs((rect.x+rect.width)-player.x); //find the distance to the side
            float xEscPlus  = (float)Math.abs(rect.x-(player.x+player.width));

            if(xEscMinus<xEscPlus) {
                player.x = rect.x+rect.width;
            } else {
                player.x = rect.x-player.width;
            }
        }
    }
}