首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

WinForm中用KeyDown事件控制 图片上下左右移动 交替方向的时候会出现停顿的 不流程如何解决

2013-11-29 
WinForm中用KeyDown事件控制 图片上下左右移动 交替方向的时候会出现停顿的 不流程怎么解决keyDown(sender

WinForm中用KeyDown事件控制 图片上下左右移动 交替方向的时候会出现停顿的 不流程怎么解决
keyDown(sender,e)
{
GO(e.Key.ToString());
}

Go里面的代码 上下左右 分别是 WSAD

public void GO(string key)
        {
            if (!this.AlloweKeys.Contains(key) || key == KeyEnum.K.ToString())
            {
                return;
            }
            //记录精灵位置
            this.Direction = key;
            //改变精灵方向
            this.Directions(key);
            double to = 0;
            Storyboard storyboard = new Storyboard();
            if (key == KeyEnum.A.ToString() || key == KeyEnum.D.ToString())
            {

                to = GetMove(key);
                //设置左右移动多少
                DoubleAnimation xAnimation = new DoubleAnimation()
                {
                    To = to,
                    Duration = new Duration(TimeSpan.FromMilliseconds(this.DoTime))
                };
                Storyboard.SetTarget(xAnimation, this);
                Storyboard.SetTargetProperty(xAnimation, new PropertyPath("(Canvas.Left)"));
                storyboard.Children.Add(xAnimation);
            }

            if (key == KeyEnum.S.ToString() || key == KeyEnum.W.ToString())
            {
                to = GetMove(key);
                //设置上下移动多少
                DoubleAnimation yAnimation = new DoubleAnimation()
                {
                    To = to,
                    Duration = new Duration(TimeSpan.FromMilliseconds(this.DoTime))
                };
                Storyboard.SetTarget(yAnimation, this);
                Storyboard.SetTargetProperty(yAnimation, new PropertyPath("(Canvas.Top)"));
                storyboard.Children.Add(yAnimation);
            }

            if (to >= 420 || to <= 0)
            {
                return;
            }
            //开始移动
            storyboard.Begin();
        } 

  
------解决方案--------------------


移动的值设置小一点看看。

热点排行