有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java 2d平台冲突错误

当角色走进一堵他们无法穿过的墙时,但是当跳进一堵墙时,玩家会被传送上下,这取决于你击中瓷砖的高度

我怎样才能让玩家在撞墙时摔倒呢

运动和碰撞检测发生在播放器中。爪哇

我认为这与签入有关,如果玩家站在一个区块上(for循环中的第一个签入),但我不确定

link to the files on github

a visual representation of my problem

以下是检查碰撞的方法:

public void collisionCheck(){
    for (ArrayList<Integer> tile: Game.tileCoordinates){
        //current row of the player
        int row = y / Game.TILE_SIZE;
        // the row below the player
        int rowBelow = y / Game.TILE_SIZE + 1;
        // the column the left of the player is in
        int columnLeft = x / Game.TILE_SIZE;
        // the column the right of the player is in
        int columnRight = (x + width) / Game.TILE_SIZE;

        // check if the player goes through the block below
        if (tile.get(1) / Game.TILE_SIZE == rowBelow){
            if ((tile.get(0) / Game.TILE_SIZE == columnLeft || 
                 tile.get(0) / Game.TILE_SIZE == columnRight)) {
                y = tile.get(1) - height;
                if (jumping) {
                    jumping = false;
                    Game.spacePressed = false;
                    dustAfterShowed = false;
                    xAfterJump = x;
                    yAfterJump = y;
                }
                if (falling){
                    falling = false;
                }
            }
        }

        // check if the player goes through the side of a block
        if (y <= tile.get(1) && y + height <= tile.get(1) + Game.TILE_SIZE &&
               y + height >= tile.get(1) && row == tile.get(1) / Game.TILE_SIZE) {
            // left side
            if (x + width >= tile.get(0) && x <= tile.get(0)) {
                x = tile.get(0) - width;
            }
            // right side
            if (x + width >= tile.get(0) + Game.TILE_SIZE && 
                    x <= tile.get(0) + Game.TILE_SIZE) {
                x = tile.get(0) + Game.TILE_SIZE;
            }
        }
        // check if the player hits a block from below
        if (tile.get(1) / Game.TILE_SIZE == row && 
               (tile.get(0) / Game.TILE_SIZE == columnLeft ||
                tile.get(0) / Game.TILE_SIZE == columnRight)){
            if (jumping) {
                y = tile.get(1) + Game.TILE_SIZE;
                velocity = 0;
            }
        }
    }

共 (1) 个答案

  1. # 1 楼答案

    我通过添加一些布尔值来修复它,当玩家从侧面撞到墙上时,它现在不再检查底部瓷砖的碰撞(使玩家摔倒,而不是停留在空中)

    检查顶部瓷砖碰撞时,请提前检查。如果不提前检查,它会检测到侧面碰撞,因为左上角和右上角的像素都在平铺中

        for (ArrayList<Integer> tile: Game.tileCoordinates){
            sideCollision = false;
            blockRight = false;
            blockLeft = false;
    
            // check for side collision, if colided then disable movement
            if ((y / Game.TILE_SIZE == tile.get(1) / Game.TILE_SIZE) &&
                    (x < tile.get(0) && x + width > tile.get(0) &&
                     x + width < tile.get(0) + Game.TILE_SIZE)) {
                sideCollision = true;
                blockRight = true;
                x = tile.get(0) - width - 1;
            }
    
            if ((y / Game.TILE_SIZE == tile.get(1) / Game.TILE_SIZE) &&
                    (x < tile.get(0) + Game.TILE_SIZE && x > tile.get(0) &&
                     x + width > tile.get(0) + Game.TILE_SIZE)) {
                sideCollision = true;
                blockLeft = true;
                x = tile.get(0) + Game.TILE_SIZE + 1;
            }
    
            // if player is about to hit head, stop upwards movement
            if ((y - velocity <= tile.get(1) + Game.TILE_SIZE &&
                     y - velocity > tile.get(1)) && ((x >= tile.get(0) &&
                     x <= tile.get(0) + Game.TILE_SIZE) ||
                    (x + width >= tile.get(0) &&
                     x + width <= tile.get(0) + Game.TILE_SIZE))){
                y = tile.get(1) + Game.TILE_SIZE;
                velocity = 0;
            }
    
            // if there is no side collision, check for bottom collision
            if (!sideCollision) {
                if ((y / Game.TILE_SIZE + 1 == tile.get(1) / Game.TILE_SIZE) &&
                        (y + height > tile.get(1)) &&
                       ((x >= tile.get(0) && x <= tile.get(0) + Game.TILE_SIZE) ||
                        (x + width >= tile.get(0) &&
                         x + width <= tile.get(0) + Game.TILE_SIZE))) {
                    y = tile.get(1) - height;
                    if (jumping) {
                        jumping = false;
                        Game.spacePressed = false;
                        dustAfterShowed = false;
                        xAfterJump = x;
                        yAfterJump = y;
                    }
                    if (falling) {
                        falling = false;
                    }
                }
            }
        }