1键盘上的Java 2播放器使用AWSD和箭头

时间:2017-09-25 21:54:37

标签: java

我正在用java制作游戏,它运行正常。但问题是,当我在地图上产生2个玩家并且我移动1个玩家时,另一个玩家同步进行。我希望player1使用(AWSD)而player2使用箭头。

这是我现在为Class编写的代码:“KeyInput”

 public void keyPressed(KeyEvent e){
        int key = e.getKeyCode();

        //If the special key is pressed, set it to true.
        if(key == KeyEvent.VK_W) handler.setUp(true);
        if(key == KeyEvent.VK_S) handler.setDown(true);
        if(key == KeyEvent.VK_A) handler.setLeft(true);
        if(key == KeyEvent.VK_D) handler.setRight(true);

        if(key == KeyEvent.VK_I) handler.setUp(true);
        if(key == KeyEvent.VK_K) handler.setDown(true);
        if(key == KeyEvent.VK_J) handler.setLeft(true);
        if(key == KeyEvent.VK_L) handler.setRight(true);
    }
    //If the pressed key is released, set everything to false.
    public void keyReleased(KeyEvent e){
        int key = e.getKeyCode();

        //If the special key is released, put it to false.
        if(key == KeyEvent.VK_W) handler.setUp(false);
        if(key == KeyEvent.VK_S) handler.setDown(false);
        if(key == KeyEvent.VK_A) handler.setLeft(false);
        if(key == KeyEvent.VK_D) handler.setRight(false);
        if(key == KeyEvent.VK_P) handler.setDropBomb(true);

        if(key == KeyEvent.VK_I) handler.setUp(false);
        if(key == KeyEvent.VK_K) handler.setDown(false);
        if(key == KeyEvent.VK_J) handler.setLeft(false);
        if(key == KeyEvent.VK_L) handler.setRight(false);
        if(key == KeyEvent.VK_M) handler.setDropBomb(true);    
    }

在课程中:“播放器”代码如下:

public Player(int x, int y, ID id, Handler handler, Game game, SpriteSheet ss) {
        super(x, y, id, ss);
        this.handler = handler;
        this.game = game;

        //Get the player image of the SpriteSheet
        player_image= ss.grabImage(1,1,32,48);
    }

    public void tick() {
        x += velX;
        y += velY;

        collision();

        //Set the movement of the player: left,right,up,down
        if(handler.isUp()) velY = -5;
        else if(!handler.isDown()) velY = 0;

        if(handler.isDown()) velY = 5;
        else if(!handler.isUp()) velY = 0;

        if(handler.isRight()) velX = 5;
        else if(!handler.isLeft()) velX = 0;

        if(handler.isLeft()) velX = -5;
        else if(!handler.isRight()) velX = 0;

        if(handler.isDropped()){
            Dropbomb(x,y);
        }

    }

我多年来一直在考虑这个问题。我试图制作第二个处理程序,但这导致了很多重复的代码。不知何故得到了一个想法?

1 个答案:

答案 0 :(得分:0)

您需要确定哪些键影响播放器1以及哪些键影响播放器2。像这样:

    //If the special key is pressed, set it to true.
    if(key == KeyEvent.VK_W) handler.setUpPlayer1(true);
    if(key == KeyEvent.VK_S) handler.setDownPlayer1(true);
    if(key == KeyEvent.VK_A) handler.setLeftPlayer1(true);
    if(key == KeyEvent.VK_D) handler.setRightPlayer1(true);

    if(key == KeyEvent.VK_I) handler.setUpPlayer2(true);
    if(key == KeyEvent.VK_K) handler.setDownPlayer2(true);
    if(key == KeyEvent.VK_J) handler.setLeftPlayer2(true);
    if(key == KeyEvent.VK_L) handler.setRightPlayer2(true);

或者你可以将玩家作为变量传递:

...
if(key == KeyEvent.VK_W) handler.setUp(true,1);
...
if(key == KeyEvent.VK_I) handler.setUp(true,2);
...

或者,如果您可以全局访问播放器变量,请执行以下操作:

...
if(key == KeyEvent.VK_W) handler.setUp(true, Global.PLAYER_1);
...
if(key == KeyEvent.VK_I) handler.setUp(true, Global.PLAYER_2);
...