WinForm:按下多个键 [英] WinForm: multiple keys pressed

查看:41
本文介绍了WinForm:按下多个键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的游戏,比如太空入侵者",但我遇到了一个问题.我试图为用户提供从左到右任意移动的可能性,同时还可以使用空格键"进行拍摄.

im working on a simple game like "space invaders",and i got into a problem. Im trying to give the user, the possiblity to move as much as he want from Left to Right, and in the same time have the possiblty to shot using the "Space bar".

我的问题是:当我按下超过 1 个键时,只有 1 个功能运行.

My problem is: when i pressed more then 1 key, only 1 function run.

这里有一些我尝试过的东西:

here a few stuff i tried:

  1. 将密钥存储在 List(但我没有找到任何执行功能的好方法,一切都变得混乱)

  1. Storing the keys in a List<Keys> (but i didnt find any good way to excute the functions and everything become messy)

2.key_down 事件的正常处理如下:

2.normal handling of the key_down event like this:

protected void Form1_keysDown(object obj, KeyEventArgs e)
{
    (e.KeyData == Keys.Space)
        spaceShip.FireBullets();

    if (e.KeyCode == Keys.Left)
        spaceShip.MoveLeft();

    if (e.KeyCode == Keys.Right)
        spaceShip.MoveRight();
 }

我的问题是:有什么好的方法可以使这项工作顺利进行?

my qustion is: what is a good way to make this work?

(对不起我的英语)

推荐答案

当您按住按键时,您依赖于键盘控制器重复按键.当您按下另一个键时,它停止工作.这需要不同的方法.

You are relying on the keyboard controller repeating the key when you hold it down. That stops working when you press another key. This requires a different approach.

首先,您需要一个枚举,用 NotMoving、MovingLeft 和 MovingRight 等值指示飞船的运动状态.将这种类型的变量添加到您的类中.您将需要 KeyDown KeyUp 事件.当您获得 KeyDown 时,例如 Keys.Left,然后将变量设置为 MovingLeft.当您获得 Keys.Left 的 KeyUp 事件时,首先检查状态变量是否仍为 MovingLeft,如果是,则将其更改为 NotMoving.

First you need an enum that indicates the motion state of the spaceship with values like NotMoving, MovingLeft and MovingRight. Add a variable of that type to your class. You'll need both the KeyDown and KeyUp events. When you get a KeyDown for, say, Keys.Left then set the variable to MovingLeft. When you get the KeyUp event for Keys.Left then first check if the state variable is still MovingLeft and, if it is, change it NotMoving.

在您的游戏循环中,使用变量值移动飞船.一些示例代码:

In your game loop, use the variable value to move the spaceship. Some sample code:

    private enum ShipMotionState { NotMoving, MovingLeft, MovingRight };
    private ShipMotionState shipMotion = ShipMotionState.NotMoving;

    protected override void OnKeyDown(KeyEventArgs e) {
        if (e.KeyData == Keys.Left)  shipMotion = ShipMotionState.MovingLeft;
        if (e.KeyData == Keys.Right) shipMotion = ShipMotionState.MovingRight;
        base.OnKeyDown(e);
    }
    protected override void OnKeyUp(KeyEventArgs e) {
        if ((e.KeyData == Keys.Left  && shipMotion == ShipMotionState.MovingLeft) ||
            (e.KeyData == Keys.Right && shipMotion == ShipMotionState.MovingRight) {
            shipMotion = ShipMotionState.NotMoving;
        }
        base.OnKeyUp(e);
    }

    private void GameLoop_Tick(object sender, EventArgs e) {
        if (shipMotion == ShipMotionState.MovingLeft)  spaceShip.MoveLeft();
        if (shipMotion == ShipMotionState.MovingRight) spaceShip.MoveRight();
        // etc..
    }

这篇关于WinForm:按下多个键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆