Search This Blog

C# -----WPF / Silverlight Game (7) The legend pathfinder

C# -----WPF / Silverlight Game (6) Perfectly move

C# -----WPF / Silverlight Game (5) Fighting




Now we can run, so how about fight.
Here it is, with photoshop put ten 150 by 150 small image together, we get a 1500 by 150 big image, let's save it as PlayerMagic.png. It is ten action details on the image.
Then add it to Player fold as we did before. Now here comes to the important tips, we need right click the image and properities, then change the copy to output directory to copy always, and change build action to embedded resource.
After rebuild the project, the Player fold with
PlayerMagic.png will be published in bin or debug fold, then BitmapFrame.Create() methods can call this image.
OK,xaml codes same as before.
here comes cs file:
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, sEventArg e) {

Spirit.Source = cutImage("PlayerMagic.png", count * 150, 0, 150, 150);

count = count == 9 ? 0 : count + 1;

}




private BitmapSource cutImage(string imgaddress, int x, int y, int width, int height) {

return new CroppedBitmap(

BitmapFrame.Create(new Uri(imgaddress, UriKind.Relative)),

new Int32Rect(x, y, width, height)

);

}

F5 again, see, magic fighting.

By the way, this codes only works on WPF, for silverlight please only use the way we discussed in (4), because net speed will slow down for png file, if silverlight supports gif files, that would be no problems at all.