Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!

I'm going to write a graphical N-puzzle using WPF.
my question is that,how could I move an image in directions that I want,with delay.

let me give you an example to clarify my question:
An image is located in center of window.and moves smoothly to left about 80 pixels in 2 seconds,then moves to down 80 pxls in 2 secs,then right,then up...

I want to know how it is done in C# code.
Posted
Comments
Storxstar 25-Oct-12 3:10am    
So you dont want to know how to do it in Xaml?
Severin Friede 25-Oct-12 3:12am    
Are you familiar with Binding and Converters? Because I would prefer to bind an element placed in a Canvas to some property and when the property is changed the elements canvas properties (left, right, top, bottom) will be animated. If you need one animation after another (like animate to right then down) then you must use animations using keyframes.

Try this code.

C#
public static void MoveTo(this Image target, double newX, double newY)
    {
        var top = Canvas.GetTop(target);
        var left = Canvas.GetLeft(target);
        TranslateTransform trans = new TranslateTransform();
        target.RenderTransform = trans;
        DoubleAnimation anim1 = new DoubleAnimation(top, newY - top, TimeSpan.FromSeconds(10));
        DoubleAnimation anim2 = new DoubleAnimation(left, newX - left, TimeSpan.FromSeconds(10));
        trans.BeginAnimation(TranslateTransform.XProperty,anim1);
        trans.BeginAnimation(TranslateTransform.YProperty,anim2);
    }
 
Share this answer
 
Comments
Severin Friede 25-Oct-12 3:39am    
Are you sure that the animations won't overlap each other resolving in a diagonal movement of the image?
Sushil Mate 25-Oct-12 4:33am    
it won't i guess.
Severin Friede 25-Oct-12 4:59am    
ehm ... then I like your solution ;)
Sushil Mate 25-Oct-12 5:00am    
thank you :)
Xaml Moving objects along a path. here is the link.


http://blogs.intuidev.com/post/2010/09/14/AnimatingObjectsAlongAPath.aspx[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900