
For Storyboard, CompositionTarget and DispatcherTimer which way you use is depands on what purpose you want to use it on.
For most of developer, they are really prefer the Storyboard because it is much more easy to use and visual. But actually there are quite difference among them.
Storyboard, normally I use it on simple object movement;
Composition Target, I use it for NPC or enemy movement or large part movement compare to whole map;
DispatcherTimer, this one is for player movement like running your warrior on the map or fighting.
This time let's talk about how to show 2D player on the screen.
First, use Photoshop create 8 images for our female wizard , only for right running behavior.
Normally, each behaviors, we need separated images for action details, some high quality game use more images on different direction same behavior, could be 8 direction or 16 direction facing for a behavior like running .
As showed on the start, our 8 images shows a right running behavior. 150 by 150 pix, named as
0.png , 1.png,.......7.png. For silverlight project, use jpg or png, for WPF project.,use gif, png.
Then, create a WPF project again, after that, add a fold call Player, put these 8 images in.
〈 Window x:Class="WpfApplication4.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WPFGame">
〈Canvas x:Name="Carrier" Width="800" Height="600" Background="Silver" /〉
〈/Window〉
CS file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace WpfApplication4
{
public partial class Window1 : Window {
int count = 1;
Image Spirit;
public Window1() {
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 = new BitmapImage((new Uri(@"Player\" + count + ".png", UriKind.Relative)));
count = count == 7 ? 0 : count + 1;
}
}
}
Then F5, your can see the female running on the screen.
One more important thing, these 8 images better have been modified on background with Alpha transparency. You will know what I am talking.