Windows窗体自定义控件键向下事件无法正常工作

时间:2014-06-10 00:46:57

标签: c# visual-studio input picturebox

我有一个小问题,我正在尝试读取c#中的键输入,键A和D工作但是Z,Space和C不工作?继承我的代码:

private async void RaceSelection_KeyDown(object sender, KeyEventArgs e)
    {
        if (movable == true)
        {
            if (e.KeyCode == Keys.A)
            {
                Character.Left -= 3;
                playerpos = Character.Location;
            }
            if (e.KeyCode == Keys.D)
            {
                Character.Left += 3;
                playerpos = Character.Location;
                if (Character.Bounds.IntersectsWith(pictureBox6.Bounds))
                {
                    if (Character.Bounds.IntersectsWith(pictureBox4.Bounds))
                    {
                        pictureBox4.Location = new Point(999999, 9999999);
                        Character.Top += 9;
                        await Task.Delay(100);
                        Character.Top += 9;
                        await Task.Delay(100);
                        Character.Top += 9;
                        await Task.Delay(100);
                        Character.Top += 9;
                        await Task.Delay(100);
                        Character.Top += 9;
                        await Task.Delay(100);
                        Character.Top += 9;
                        await Task.Delay(100);
                        Character.Top += 5;
                        this.Controls.Clear();
                        this.Controls.Add(GapFiller1);

                        dungeonone();

                    }
                }
                if (e.KeyCode == Keys.Space)
                {
                    Character.Top -= 25;
                    await Task.Delay(125);
                    Character.Top += 25;
                }
                if (e.KeyCode == Keys.C)
                {
                    if (Character.Bounds.IntersectsWith(pictureBox5.Bounds))
                    {
                        TownOne();
                    }
                }

                if (e.KeyCode == Keys.Z)
                {
                    if (Character.Bounds.IntersectsWith(pictureBox6.Bounds))
                    {

                        if (EntityVar.DraugarHealth == 1)
                        {
                            Engine.EntityVar.Draugartakesdamage = false;

                            this.Controls.Remove(pictureBox6);
                            LastKill = "Draugar";
                            label3.Text = "Well Done! but what is that ladder over there???";
                        }
                        else
                        {
                            Engine.EntityVar.DraugarHealth -= 1;
                            if (Engine.EntityVar.Draugartakesdamage == true)
                            {
                                this.Controls.Add(HitMarker);
                                HitMarker.BringToFront();
                                await Task.Delay(250);
                                this.Controls.Remove(HitMarker);
                            }

                        }

                    }
                }
            }
        }
    }

任何人都知道我做错了什么?顺便说一下,我正在制作一款游戏,我非常清楚我不是最好的方式,但是哦,那就是我这样做:D,无论如何,如果你知道为什么它不起作用我会非常感激。

1 个答案:

答案 0 :(得分:1)

问题似乎在于您只是错误地将代码括起来。在您给定的代码中,Z,C和Space键处理程序实际上是 in D处理程序。我假设您不希望这种逻辑存在,所以将它们移到D处理程序之外,它将解决您的问题。

另外,使用开关或者如果是这样的块:)