【问题标题】:Why does the second rectangle collision not work when the first does?为什么第二个矩形碰撞在第一个矩形碰撞时不起作用?
【发布时间】:2015-11-10 04:08:28
【问题描述】:

我不知道为什么会这样。第一次碰撞完美。如果我触底,它就会停止。如果我到达顶部,它就会停在顶部。但是您可以逐步通过第二个平台。有人知道它为什么这样做吗?抱歉,java新手。我应该提一下,它是我遇到问题的第二个平台的底部。

public void customUpdate() {  // Remember, all logic goes in here
        if(charX > 780) {
            charX = 0;
        }
        if(charX < 0) {
            charX = 780;
        }
        if(gravity) {
            charVY += 1;
            charY += charVY;
        }
        if(charY > 670-15) {
            charY = 0;
        }
        if(buttonOne) {
            exit = true;
        }

        if(charY+30 >= wallY) {
            charY = wallY-30;
            charVY = 0;
            wallCollision = true; // numOfJumps set to 0 because it increases by 1 every time you jump, allowing as many jumps as the programmer desires
            numOfJumps = 0;
            secHasPassed = false;
        }

        if(charY < platOneY &&
                charX < platOneX + platOneW && // Collision for plat 1 and so on
                charX + charW > platOneX &&
                charY < platOneY + platOneH &&
                charH + charY > platOneY) { //TODO  Get it so that it stops him when he hits the bottom
            charY = platOneY-30;
            numOfJumps = 0;
            charVY = 0;
            wallCollision = true; // Gets top of the platform collision
        }

        if(charY > platOneY &&
                charX < platOneX + platOneW && // Collision for plat 1 and so on
                charX + charW > platOneX &&
                charY < platOneY + platOneH &&
                charH + charY > platOneY) { //TODO  Get it so that it stops him when he hits the bottom
            charY = platOneY+platOneH+1;
            numOfJumps = maxJumps;
            charVY = 0;
            wallCollision = true; // Gets bottom of platform collision
        }

        if(charY > platTwoY &&
                charX < platTwoX + platTwoW &&
                charX + charW > platTwoX &&
                charY < platTwoY + platTwoH &&
                charH + charY > platTwoY) {
            charY = platTwoY+platTwoH+1;
            numOfJumps = maxJumps;
            charVY = 0;
            wallCollision = true;
            System.out.println("Bottom");
        }

        if(charY < platTwoY &&
                charX < platTwoX + platTwoW &&
                charX + charW > platTwoX &&
                charY < platTwoY + platTwoH &&
                charH + charY > platTwoY) {
            charY = platTwoY-30;
            numOfJumps = 0;
            charVY = 0;
            wallCollision = true;
        }

        if(charX < platThreeX + platThreeW &&
                charX + charW > platThreeX &&
                charY < platThreeY + platThreeH &&
                charH + charY > platThreeY) {
            charY = platThreeY-30;
            numOfJumps = 0;
            charVY = 0;
            wallCollision = true;
        }

        if(canMoveL) { // Movement Logic
            charX -= 5;
        }
        if(canMoveR) {
            charX += 5;
        }
        if(charVY < -15) {
            charVY = -15;
        }

    }

【问题讨论】:

    标签: java collision-detection rectangles


    【解决方案1】:
    if (charY > platTwoY - 15) {
           charX < platTwoX + platTwoW &&
           charX + charW > platTwoX &&
           charY < platTwoY + platTwoH &&
           charH + charY > platTwoY) {
               charY = platTwoY + platTwoH + 1;
               charVY = 0;
               numOfJumps = maxJumps;
               wallCollision = true;
    }
    

    我让它工作了。我不知道为什么我没有发现这个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 2014-03-18
      • 1970-01-01
      相关资源
      最近更新 更多