开发者

C# move a Polyline

I'm new in the graphical programming with C#. Yesterday I started one new project (WPF). There is a Polyline object which I have to move along the screen with coordinates which I'm calculating. I don't know how to move the object and make something like an animation. On mouse d开发者_开发知识库own I want to start this method Move() after that go into the while cycle and when the condition is completed (end == true) I want to end the cycle and complete the animation. And while I'm in the cycle my idea is to move my Polyline with slow movements. I tried to put Move() into a thread and use Thread.Sleep(...); but I could see just the end position not all way of the Polyline. I tried to put it into new Thread(new ThreadStart(Move)); ...and this.Dispatcher.BeginInvoke, the effect was the same. Could you tell me, please, how I can make this movement?

     public void Move()
     {
        bool end = false;
        while (!end)
        {
            double x = lastPosX;
            double y = lastPosY;

            double a = y1 - y;
            double b = x - x1;
            double c = -x * y1 + x1 * y;

            double u, v;
            GetC(out u, out v);                    

            if (y1 < lastPosY)
            {
                GetCoordinates(ref u, ref v);
            }

            if (u > width || v > height)
            {
                 gameEnd = true;
            }

            lastPosX = u;
            lastPosY = v;

            p.Points.Remove(p.Points.First());
            p.Points.Add(new Point(u, v));

          }

       }


I wasn't quite able to figure out how your Move method works but here's an example of how you can move a Polyline from Left to Right upon MouseDown. Hopefully you'll be able to adapt it to your needs

Xaml

<Canvas Name="myCanvas">
    <Polyline Name="myPolyline"
              MouseDown="Polyline_MouseDown"
              Canvas.Left="75"
              Canvas.Top="50"
              Points="25,25 0,50 25,75 50,50 25,25 25,0" 
              Stroke="Blue"
              StrokeThickness="10"/>
</Canvas>

Code behind

private void Polyline_MouseDown(object sender, MouseButtonEventArgs e)
{
    double left = Canvas.GetLeft(myPolyline);
    var animationThread = new Thread(new ThreadStart(() =>
    {
        while (left < 300)
        {
            left += 10;
            // SetLeft is done in the UI thread
            Dispatcher.Invoke(new Action(() =>
            {
                Canvas.SetLeft(myPolyline, left);
            }));
            Thread.Sleep(50);
        }
    }));
    animationThread.Start();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜