简体   繁体   English

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

[英]Collision bug in a 2D platform game

I'm new to java, and game programming and I'm starting my first big project which is a 2D platform puzzle game. 我是Java和游戏编程的新手,并且正在启动我的第一个大项目,这是一个2D平台益智游戏。 This is my player movement code 这是我的玩家移动代码

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

}

and this is the collision code 这是碰撞代码

public void checkCollision(Rectangle rect){
   if (rect.intersects(r)){
      if(Player.movingRight){
      Player.centerX = tileX + 11;
      Player.speedX =0;
      }

      if(Player.movingLeft){
      Player.centerX = tileX + 89;
      Player.speedX = 0;
      }

      if(Player.speedY > 0){
         Player.centerY = tileY - 25;
         Player.speedY = 0;
         Player.jumped = false;
      }
   }

}

There are two problems.The first one is that if I press one of the movement keys when landing the character "teleports" to the right or left. 有两个问题。第一个问题是,如果在将字符“电传”着陆到右侧或左侧时按下移动键之一。 I know this happens because I programmed it that if the character intersects with the ground while movingRight or movingLeft are true he moves right or left.(I made it this way so the horizonal collision will work) and I cant think of any other way to do it or how to fix it. 我知道发生这种情况是因为我编写了这样的程序:如果在moveRight或movingLeft为真的情况下角色与地面相交,则他会向右或向左移动(我这样做是为了使水平碰撞有效),我想不出其他任何方法做到这一点或如何解决。

The second problem is that if the character moves of a platfrom he does not fall down. 第二个问题是,如果角色从平台上移开,他不会跌倒。 I tryed to fix it by adding to the collision method 我试图通过添加碰撞方法来修复它

else{
speedY += 1;
}

But it made the character disappear for some reason. 但是由于某种原因,它使角色消失了。

Thanks a lot! 非常感谢!

This code was originally written in C++ for a 3D platformer. 该代码最初是用C ++编写的,用于3D平台。 I rewrote it, but there might be some bugs. 我重写了它,但是可能存在一些错误。 I can draw a picture later if it's difficult to understand. 如果很难理解,我以后可以画一幅画。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM