【问题标题】:Java Game Collision Slick2DJava 游戏碰撞 Slick2D
【发布时间】:2014-02-05 22:38:35
【问题描述】:

我正在使用 Slick 和 Tiled2D 制作一个自上而下的 RPG 风格游戏。我正在尝试进行碰撞,但玩家似乎能够穿过建筑物左侧的块或块的左半部分。完整的项目在这里:http://github.com/Cj1m/RPG 这是我的玩家移动和碰撞检测代码:

@Override
public void update(GameContainer gc, int delta) throws SlickException {
    input = gc.getInput();
        if(input.isKeyDown(Input.KEY_W)){
            if(!isBlocked(x, y - delta * 0.1f)){
                y-=0.1 * delta;
                timer(0,1,delta);
            }
        }else if(input.isKeyDown(Input.KEY_S) && y < screenBottomEdge){
            if (!isBlocked(x, y + playerHeight + delta * 0.1f)){
                y+=0.1 * delta;
                timer(0,0,delta);
            }
        }else if(input.isKeyDown(Input.KEY_A) && x > screenLeftEdge){
            if (!isBlocked(x - delta * 0.1f, y)){
                x-=0.1 * delta;
                timer(0,2,delta);
            }
        }else if(input.isKeyDown(Input.KEY_D) && x < screenRightEdge){
            if (!isBlocked(x + playerWidth + delta * 0.1f, y)){
                x+=0.1 * delta;
                timer(0,3,delta);
            }
        }
}

private boolean isBlocked(float x, float y) {
    int xBlock = (int)x / 30;
    int yBlock = (int)y / 30;
    return blocked[xBlock][yBlock];
}

【问题讨论】:

  • 使用 (int) _ / 30 的语句可能返回 0,因为您正在除以整数。
  • 在 isBlocked 中,您的投射太快了。尝试浮动 xBlock = x/30; (对于 yBlock 也是一样),然后在 return 中,尝试 return blocks[(int) xBlock][(int) yBlock];

标签: java collision slick2d


【解决方案1】:

我猜 isBlocked 中的 x 和 y 是玩家的像素精确的 x 和 y 位置,而 xBlock 和 yBlock 是图块精确的位置。

当你说他们通过了一半的街区时,这在我看来就像是一个四舍五入的问题

int xBlock = (int)x / 30;
int yBlock = (int)y / 30;

尝试一些类似的东西

(int) Math.round(x/30.0);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多