如何检测游戏控制的多个键

时间:2014-05-09 12:17:40

标签: java

我正在制作一个足球比赛。我的问题是当我按下某个键并更改按键以按下我的播放器类型的停止然后响应。就像我要向左,我按下向上按钮。我认为我的问题是因为我不支持按下多个键一次。所以我需要帮助我如何实现这个。这是我的代码

//add keylistener to window frame
    window.addKeyListener(new KeyAdapter() {

        @Override
        public void keyReleased(KeyEvent e) {

            switch (e.getKeyChar()) {
                case KeyEvent.VK_ESCAPE: {

                        window.setVisible(false);
                        TeamMenu.setVisible(true);
                        MainMenu.setVisible(false);
                        g_SoccerPitch.canUpDate = false;


                }
                break;
                case 'r':
                case 'R': {
                    SoccerPitchLock.lock();
                    g_SoccerPitch = null;
                    g_SoccerPitch = new SoccerPitch(cxClient, cyClient);
                    SoccerPitchLock.unlock();
                }
                break;

                case 'p':
                case 'P': {
                    g_SoccerPitch.TogglePause();
                }
                break; 
            }//end switch
        }//end switch        }

        @Override
        public void keyPressed(KeyEvent e) {

            if(g_SoccerPitch.GameOn() && g_SoccerPitch != null){

            switch (e.getKeyChar()) {

                case 'w':
                case 'W':{
                    g_SoccerPitch.UserControlledTeam.UserControlledPlayer().MoveUp();
                    chaseBall();
                }
                break;

                case 's':
                case 'S':{
                    g_SoccerPitch.UserControlledTeam.UserControlledPlayer().MoveDown();
                   chaseBall();
                }
                break;

                case 'a':
                case 'A':{
                    g_SoccerPitch.UserControlledTeam.UserControlledPlayer().MoveDown();
                    chaseBall();
                }
                break;

                case 'd':
                case 'D':{
                    g_SoccerPitch.UserControlledTeam.UserControlledPlayer().MoveRight();
                    chaseBall();
                }
                break;

                case 'm':
                case 'M':{
                     //shoot ball if ball withinin kicking range and team is in control
                     if(g_SoccerPitch.UserControlledTeam.InControl() && g_SoccerPitch.UserControlledTeam.UserControlledPlayer().BallWithinKickingRange()){
                      g_SoccerPitch.UserControlledTeam.UserControlledPlayer().UserPlayerShootBall();
                     }   
                }
                break;

                case 'l':
                case 'L':{
                     //if team in control pass the ball else chase the ball
                     if(g_SoccerPitch.UserControlledTeam.InControl()){

                      if(g_SoccerPitch.UserControlledTeam.UserControlledPlayer().BallWithinReceivingRange()){
                      g_SoccerPitch.UserControlledTeam.UserControlledPlayer().UserPlayerPassBall();
                       }
                        }  else{

                    g_SoccerPitch.UserControlledTeam.UserControlledPlayer().Steering().SeekOn();
                    g_SoccerPitch.UserControlledTeam.UserControlledPlayer().Steering().SetTarget(g_SoccerPitch.Ball().m_vPosition);
                    if(g_SoccerPitch.UserControlledTeam.UserControlledPlayer().AtTarget()){
                    g_SoccerPitch.UserControlledTeam.UserControlledPlayer().Steering().SeekOff();
                    }
                     } 
                }
                break;

            }//end switch
        }//end switch  
        }//closes if           
    });

1 个答案:

答案 0 :(得分:1)

  

您可以为所有键设置布尔变量。按下键时设置值为true,当该键释放时设置为false。下面是我在C#中的代码,用于向上和向左

bool isleft = false;
    bool isright = false;
    bool isup = false;
    private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        if (e.KeyData == Keys.Left)
            isleft = true;
        if (e.KeyData == Keys.Up)
            isup = true;
        if (e.KeyData == Keys.Up)
        {
            if (isleft)
            {
                moveleft();
            }
            moveup();
        }
        if (e.KeyData == Keys.Left)
        {
            if (isup)
            {
                moveup();
            }
            moveleft();
        }
    }
    public void moveleft()
    {
        button1.Location = new Point(button1.Location.X - 1, button1.Location.Y);
    }
    public void moveup()
    {
        button1.Location = new Point(button1.Location.X , button1.Location.Y-1);
    }

    private void button1_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyData == Keys.Left)
            isleft = false;
        if (e.KeyData == Keys.Up)
            isup = false;
    }

通过这种方式,您可以保持所有动作。我对java syntex并不擅长,所以希望这会有所帮助