C # development Wpf/silverlight animation and game series Tutorials (Game Course): (v) to achieve 2D character animation ②
The second method I call the picture interception method, prepares to work: Here I create the protagonist to the right direction to cast the animation as an example. First of all, you need to 150*150 10 frames of the picture through Photoshop or other ways to become a 1500*150 big picture, the following figure:
It is clear from the diagram that the main character's entire flow of the casting process. Then save the picture to the Bindebug folder in the project folder, as shown in the following figure:
Why do we have to put it here? Because the Bitmapframe.create () function calls the relative path picture in the following code to only recognize the folder, why? Ask MS
Ok,xaml code is still the same as in the previous chapters, then the following is the background C # code:
Image Spirit;
int count = 1;
Public Window5 () {InitializeComponent ();
Spirit = new Image ();
Spirit.width = 150;
Spirit.height = 150;
CARRIER.CHILDREN.ADD (Spirit);
DispatcherTimer DispatcherTimer = new DispatcherTimer ();
Dispatchertimer.tick + = new EventHandler (Dispatchertimer_tick);
Dispatchertimer.interval = Timespan.frommilliseconds (150);
Dispatchertimer.start (); } private void Dispatchertimer_tick (object sender, EventArgs e) {spirit.source = Cutimage (@ "PLAYER\PL
Ayermagic.png ", Count * 150, 0, 150, 150); Count = Count = 9?
0:count + 1; ///<summary>///intercepting pictures///</summary>///<param name= "imgaddress" > FileName (including address + extension) </param>///<param name= "x" > upper left corner x</param>///<param name= "y" > upper left corner y< /param>///<paramName= "width" > Captured picture width </param>///<param name= "height" > truncated picture high </param>///<RETURNS&G
t; After intercepting image data source </returns> private BitmapSource cutimage (string imgaddress, int x, int y, int width, int height) {
return new Croppedbitmap (Bitmapframe.create (imgaddress, urikind.relative)),
New Int32Rect (x, y, width, height)); }