1

So in short i'm simply trying to move a rectangle around a Canvas object in a WPF application. What i have here is my KeyDown event function. The problem is, when i hold a key down for long, it launches this function over and over again rapidly and screws up my rectangle location code.

My theory/logic behind it: BECAUSE WHEN YOU HOLD A BUTTON DOWN ON A KEYBOARD IT DOES NOT MOVE SMOOTHLY (TEST IT ON THE SCROLL BAR IN YOUR BROWSER, IT STARTS, pauses, THEN CONTINUES SMOOTHLY), i want it to start a forms timer that moves the object in the UI. Then when the KeyUp event happens, the timer STOPS.

public void Window_KeyDown(object sender, KeyEventArgs e)
{
    string msg;
    string keystr = e.Key.ToString();
    Key keyval = e.Key;


    switch (keystr)
    {
        case "Down":
            Console.WriteLine("Case 1");
            Display.Content = "Down";
            foreach (Character character in creatures)
            {
                //character.buttondown = true;
                character.Position("Down");
            }
            break;
        case "Up":
            Console.WriteLine("Case 2");
            Display.Content = "Up";
            foreach (Character character in creatures)
            {
                //character.buttondown = true;
                character.Position("Up");
            }
            break;
        case "Left":
            Console.WriteLine("Case 3");
            Display.Content = "Left";
            foreach (Character character in creatures)
            {
                //character.buttondown = true;
                character.Position("Left");
            }
            break;
        case "Right":
            Display.Content = "Right";
            foreach (Character character in creatures)
            {

                //character.buttondown = true;
                character.Position("Right");
            }
            break;
    }
}

public void Window_KeyUp(object sender, KeyEventArgs e)
{
    Display.Content = "No key is pressed.";
    foreach (Character character in creatures)
    {
        if (e.Key == Key.Right)
        {
            character.StopIt();
        }
        if (e.Key == Key.Left)
        {
            character.StopIt();
        }
        if (e.Key == Key.Up)
        {
            character.StopIt();
        }
        if (e.Key == Key.Down)
        {
            character.StopIt();
        }

    }
}

and just for reference if you need my rectangle class code i'll post what happens if the RIGHT arrow key is pressed:

  1. Position is called

    public void Position(String Direction)
    {
    
        if (Direction == "Right")
        {
            tmr = new System.Windows.Forms.Timer();
            tmr.Interval = this.waitTime;
            tmr.Tick += new EventHandler(GoRight);
            tmr.Start();
        }
     }
    
  2. GoRight is called:

    public void GoRight(object sender, System.EventArgs e)
    {
        if (x < Background.ActualWidth - CharacterWidth)
        {
            if (goRight)
            {
                x += incrementSize;
                CharacterImage.SetValue(Canvas.LeftProperty, x);
    
            }
            if (x > Background.ActualWidth - CharacterWidth)
            {
                goRight = false;
                tmr.Stop();
            }
        }
    
    }
    

Finally, StopIt is called in the KeyUp event:

public void StopIt()
{
    tmr.Stop();
    goRight = true;
    goLeft = true;
    goUp = true;
    goDown = true;
}

I've only been learning c# for a couple months now so i'm trying to keep it relatively simple if possible, and only use .net.

Any help would be appreciated!!

EDIT:: MY SOLUTION:

I simply made a while(flag) loop around my switch case. Then i set flag = false within the cases. When Key UP is pressed i set flag equal to true again. YAY

4

2 回答 2

0

I assume that you want your character to move on the initial KeyDown event. Then you want to ignore any subsequent KeyDown events until you get a KeyUp event.

So you can ignore the subsequent KeyDown events by checking e.IsRepeat e.g.

public void Window_KeyDown(object sender, KeyEventArgs e)
{
    if (e.IsRepeat) return;

    // rest of your code...

BTW, the non-smooth movement that you observe when scrolling an application is caused by the keyboard repeat delay. You can set this in the keyboard properties or though http://msdn.microsoft.com/en-us/library/system.windows.systemparameters.keyboarddelay.aspx

于 2013-11-06T23:52:42.067 回答
0

我对 WPF 的了解还不够,无法告诉您发生了什么,尽管您对按钮/暂停的事情可能是正确的。这取决于 WPF 如何处理按键。我的猜测是它的工作方式与大多数 Microsoft 表单的工作方式相同。它有一个暂停,以防止您一次输入多个字符。可能有办法解决这个问题,但我不确定。

我要说的是你应该使用为游戏设计的东西。当我第一次尝试创建游戏时,我的风格与您正在做的类似,但它不起作用。您使用的软件是专为办公软件设计的,不会让您访问所需的软件;至少不是没有战斗和变通办法。正如 Alex Beisley 所建议的那样,研究 XNA。不幸的是,这是一种死语言,但它最近就死了。它使用 c#,由 Microsoft 制作,功能强大,无需与您争吵即可完成您想做的事情,并且一旦掌握了窍门,就可以轻松使用。看到它被杀死真是可惜。

如果您想折磨自己,那么我建议您走我一直在尝试的路线,即学习 C++ 和 DirectX。这并不容易,您需要耐心阅读多个教程(似乎没有一个教程能很好地解释任何内容)。DirectX 和 C++ 不会很快消失,因此如果您希望进入一门长期语言,它们是一个安全的选择。

于 2013-11-06T22:54:50.223 回答