【问题标题】:Java 2D Game Key Input (Best Practice?)Java 2D 游戏键输入(最佳实践?)
【发布时间】:2016-07-23 23:14:40
【问题描述】:

我是游戏开发的新手,并选择开始开发 2D 自上而下的滚动游戏。我正在为这个游戏使用 Slick2D 库。

我的问题是关于为精灵移动采用多方向输入的最佳实践(上 + 右 = 对角线)

目前,我有一个相当难看的 if/elseif 链来读取来自键盘的输入,然后在“Mob”类中检查以确定精灵将如何移动。当前的设置工作得很好,但我的问题是,是否有另一种更好的方法来为对角线(或任何键组合)获取多个输入

这里是读取输入的主类的更新方法(blooper 是 'Mob' 的实例):

public void update(GameContainer container, StateBasedGame arg1, int delta) throws SlickException {
    Input input = container.getInput();


    if(input.isKeyDown(Input.KEY_RIGHT)) {          //RIGHT
        if(input.isKeyDown(Input.KEY_UP)){          //RIGHT + UP
            blooper.direction = 2;
        } else if(input.isKeyDown(Input.KEY_DOWN)){ //RIGHT + DOWN
            blooper.direction = 3;
        }
        else {
            blooper.direction = 1;
        }
    } else if(input.isKeyDown(Input.KEY_LEFT)){     //LEFT
        if(input.isKeyDown(Input.KEY_UP)){          //LEFT + UP
            blooper.direction = 5;
        } else if(input.isKeyDown(Input.KEY_DOWN)){ //LEFT + DOWN
            blooper.direction = 6;
        } else{
            blooper.direction = 4;
        }
    } else if(input.isKeyDown(Input.KEY_UP)){       //UP
        if(input.isKeyDown(Input.KEY_RIGHT)){       //UP + RIGHT
            blooper.direction = 8;
        } else if(input.isKeyDown(Input.KEY_LEFT)){ //UP + LEFT
            blooper.direction = 9;
        } else{
            blooper.direction = 7;
        }
    } else if(input.isKeyDown(Input.KEY_DOWN)){     //DOWN
        if(input.isKeyDown(Input.KEY_RIGHT)){       //DOWN + RIGHT
            blooper.direction = 11;
        } else if(input.isKeyDown(Input.KEY_LEFT)){ //DOWN + LEFT
            blooper.direction = 12;
        } else{
            blooper.direction = 10;
        }
    } else{
        blooper.direction = -1;
    }

    blooper.update(delta);

}

这是在 Mob 类中处理输入的方式:

public class Mob {

private final int RIGHT     = 1;
private final int RIGHTUP   = 2;
private final int RIGHTDOWN = 3;
private final int LEFT      = 4;
private final int LEFTUP    = 5;
private final int LEFTDOWN  = 6;
private final int UP        = 7;
private final int UPRIGHT   = 8;
private final int UPLEFT    = 9;
private final int DOWN      = 10;
private final int DOWNRIGHT = 11;
private final int DOWNLEFT  = 12;
private final int IDLE      = -1;

int direction = IDLE;

int x, y;
Image sprite;

public Mob() throws SlickException{
    x = 20;
    y = 20;
    sprite = new Image("res/blooper.png");
}

public void update(int delta){
    move();
}

public void draw(){
    sprite.draw(x, y);
}

public void move(){

    switch(direction){
        case RIGHT:
            x += 1;
            break;
        case RIGHTUP:
            x += 1;
            y -= 1;
            break;
        case RIGHTDOWN:
            x += 1;
            y += 1;
            break;          
        case LEFT:
            x -= 1;
            break;
        case LEFTUP:
            x -= 1;
            y -= 1;
            break;
        case LEFTDOWN:
            x -= 1;
            y += 1;
            break;
        case UP:
            y -= 1;
            break;
        case UPRIGHT:
            y -= 1;
            x += 1;
            break;
        case UPLEFT:
            y -= 1;
            x -= 1;
            break;
        case DOWN:
            y += 1;
            break;
        case DOWNRIGHT:
            y += 1;
            x += 1;
            break;
        case DOWNLEFT:
            y += 1;
            x -= 1;
            break;
        case IDLE:
            //nothing
        }
    }
}

就像我说的...这可行,但似乎不是最好的方法。有什么建议吗?

【问题讨论】:

    标签: java input keypress slick2d 2d-games


    【解决方案1】:

    使左/右和上/下运动独立。您(或框架 - 我不熟悉它)似乎使用 x/y 坐标,这些坐标在数学上是独立的。 因此,制作一个处理上/下运动的函数和一个处理左/右运动的函数,您就可以摆脱它们的“组合”。方向是正整数有什么原因吗?尝试让left = -1,right = 1,并且没有x方向移动0,然后对y做同样的事情。这应该会使其更直观。

    愉快的编码

    【讨论】:

    • 感谢您的建议 - 不知道为什么我试图将它们组合成一个 move() 方法。这应该可行 - 我会制定更改并为您提供更新!再次感谢!
    【解决方案2】:

    按位创建blooper.direction,例如:

    blooper.direction=0;
    if(input.isKeyDown(Input.KEY_RIGHT)) blooper.direction|=1;
    if(input.isKeyDown(Input.KEY_LEFT)) blooper.direction|=2;
    if(input.isKeyDown(Input.KEY_UP)) blooper.direction|=4;
    if(input.isKeyDown(Input.KEY_DOWN)) blooper.direction|=8;
    

    然后在move 中移动,例如:

    if ((blooper.direction&1)!=0) x+=1;
    if ((blooper.direction&2)!=0) x-=1;
    if ((blooper.direction&4)!=0) y-=1;
    if ((blooper.direction&8)!=0) y+=1;
    

    【讨论】:

    • 我喜欢这种更简约的方法。我会看看我是否可以实现这个并给你一个更新。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    相关资源
    最近更新 更多