【问题标题】:How to detect multiple keys for a Game Control如何检测游戏控件的多个键
【发布时间】:2014-05-09 20:34:18
【问题描述】:

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

//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           
    });

【问题讨论】:

  • 所以我猜你希望能够沿对角线移动?按键始终是单个原子界面输入。最好的办法是看看如何同时抓住两台压力机,以及需要什么样的设计!

标签: java


【解决方案1】:

您可以为所有键设置布尔变量。按下键时设置值为真,释放该键时设置值为假。下面是我在 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 所以希望这会有所帮助

【讨论】:

  • 是 button1_PreviewKeyDown(....) 方法和 java keyPressed 或 keyReleased 方法一样
猜你喜欢
  • 2011-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多