日期:2014-05-17  浏览次数:20947 次

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()
                {
       &