2) compositiontarget
The second method to create animation is by compositiontarget. in official document, compositiontarget object can create M animation by each frame's callback. in other words, the animation created by compositiontarget is base on the event fired by UI redrawing, Which is accordance with the UI refresh frequency, so the frequency is fixed, it is difficult to control it by ourselves.
But how to use it? The XAML file is the same as the one in the previous chapter, the difference is in the code-behind, as follows:
Public partial class Mainpage : Usercontrol { Private Rectangle Rect; // Set the moving speed Double Speed = 5; // Set the Moving Target Point MoveTo; Public Mainpage () {initializecomponent (); rect = New Rectangle () {Fill = New Solidcolorbrush ( Colors . Red), width = 50, Height = 50, radiusx = 5, radiusy = 5}; carrier. Children. Add (rect );Canvas . Setleft (rect, 0 ); Canvas . Settop (rect, 0 ); // Register UI refresh event Compositiontarget . Rendering + = compositiontarget_rendering ;} Void Compositiontarget_rendering ( Object Sender, Eventargs E ){ Double Rect_x = Canvas . Getleft (rect ); Double Rect_y = Canvas . Gettop (rect ); Canvas . Setleft (rect, rect_x + (rect_x <moveTo. X? Speed:-speed )); Canvas . Settop (rect, rect_y + (rect_y <moveTo. Y? Speed:-speed ));} Private void Carrier_mouseleftbuttondown ( Object Sender, Mousebuttoneventargs E) {moveTo = E. getposition (carrier );}}
1st, We must register rendering event of the compositiontarget as follows:
Compositiontarget. Rendering + = compositiontarget_rendering;
2nd, because we want to make the rectangle move to whatever we click, so I use a variable named moveTo to save the position we clicked, and assign the value to it in the mouse click envet, as we can see in the method carrier_mouseleftbuttondown.
3rd, I create animation in the method compositiontarget_rendering, because it is an event fired by UI's refreshment all the time, I dynamically set the rectangle's left and top properties to simulate the moving.
Notice that the speed is minus or not in this method, it depends on the relationship between the new position and the old one. it is suitable to human's feeling, for example, if you click on the left to the rectangle, The rect_x will be more than moveTo. x, so the speed will be minus, which makes rect_x lower in X direction, it means the rectangle move left to the original position. is that right? Of course.
Please press Ctrl + F5, click anywhere in the window, you will find the rectangle move as we expected before.
But, we find a critical problem in this mechanic. The screen is trembled heavily, and the moving is not smooth. Is anything wrong in our code?
The reason that the rectangle can not move smoothly, is that the speed in the X and Y direction are different, so we need to use math. sin and math. cos function to separate original speed into speed_x and speed_y, the key syntax is as follows:
Void Compositiontarget_rendering ( Object Sender, Eventargs E ){ Double Rect_x =Canvas . Getleft (rect ); Double Rect_y = Canvas . Gettop (rect ); Canvas . Setleft (rect, rect_x + (rect_x <moveTo. X? Speed_x:-speed_x )); Canvas . Settop (rect, rect_y + (rect_y <moveTo. Y? Speed_y:-speed_y ));} Private void Carrier_mouseleftbuttondown ( Object Sender, Mousebuttoneventargs E ){ Double Rect_x = Canvas . Getleft (rect );Double Rect_y = Canvas . Gettop (rect); moveTo = E. getposition (carrier ); Double Len = Math . SQRT ( Math . Pow (moveTo. X-rect_x, 2) + Math . Pow (moveTo. Y-rect_y, 2); speed_x = Math . Abs (speed * (moveTo. X-rect_x)/Len); speed_y = Math . Abs (speed * (moveTo. Y-rect_y)/Len );}
The reason that the screen is trembled heavily, depend on the speed value. now the speed is 5. let's reduce its value, set to 1. the tresponing is slightly, but still existing. why the value is more large, the more heavily the screen is trembled? You can find the root cause in compositiontarget_rendering function.
As you know, compositiontarget_rendering is a non-stop function. So the sentence
Canvas. Setleft (rect, rect_x + (rect_x <moveTo. X? Speed_x:-speed_x ));
Will be executed each time, even the rectangle has arrived the target. it doesn' t matter. the worst scenario is that the rectangle doesn't arrive the target forever. for example, the rect_x is 100, the moveTo. X is 101, the speed_x is 3, then the left value of the rectangle will be set to 103,100,103,100, it means the rectangle won't stop. so does the scenario in Y direction. that is why you see the screen trembled.
The solution to this issue is to set a critical value, the value is half of the speed. so when the distance between rect_x and moveTo. X is less than the critical value, we will treat the rectangle has already arrived the target in X direction, soCanvas. Setleft sentence won't be executed; otherwise it will be executed, which make the rect_x tend to the moveTo. x gradually.
so does the solution in Y direction. I modify the compositiontarget_rendering again, as follows:
VoidCompositiontarget_rendering (ObjectSender,EventargsE ){DoubleRect_x =Canvas. Getleft (rect );DoubleRect_y =Canvas. Gettop (rect );If(Math. Abs (rect_x-moveTo. X)> speed_x/2)Canvas. Setleft (rect, rect_x + (rect_x <moveTo. X? Speed_x:-speed_x ));If(Math. Abs (rect_y-moveTo. Y)> speed_y/2)Canvas. Settop (rect, rect_y + (rect_y <moveTo. Y? Speed_y:-speed_y ));}
Now the animation looks like normal as before.
Summary: This chapter introduce how to use compositiontarget to create animation base on frames, it is different from the traditional animation by switching pictures. its principle is base on change the object's property all the time.
Next chapter, I will introduce the 3rd method to create animation, and compare all these methods. Please focus on it.
Chinese friend, You can also visit this Chinese blog if you feel difficult to read English, http://www.cnblogs.com/alamiye010/archive/2009/06/17/1505331.html, part of my article is base on it.
Demo download: http://silverlightrpg.codeplex.com/releases/view/40978