越野车碰撞2D平台游戏

时间:2014-05-23 10:12:11

标签: java game-engine collision

我正在建立我的第一个大项目,我遇到了碰撞问题。 起初我在碰撞检测中使用了println命令并且效果很好,所以我认为问题不在于检测本身。

这个动作代码:

public void update() {
    // Moves Character or Scrolls Background accordingly.

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

}

这是碰撞代码:

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

问题在于它非常有缺陷,有时玩家会“传送”甚至摔倒在地上。

0 个答案:

没有答案