Silverlight制作逐幀動畫 v2 - part2
private string[] shippics = new string[] { "Ship1/1.png", "Ship1/2.png", "Ship1/3.png", "Ship1/4.png" };
protected DateTime lastTick;
protected TimeSpan elapsed;
private double lasrFrameTime = 0;
private int currentFrame = 0;
private double fps = 15;
public MainPage()
{
InitializeComponent();
lastTick = DateTime.Now;
CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
}
void CompositionTarget_Rendering(object sender, EventArgs e)
DateTime now = DateTime.Now;
elapsed = now - lastTick;
lasrFrameTime += elapsed.TotalSeconds;
if (lasrFrameTime >( 1 / fps))
{
lasrFrameTime = 0;
currentFrame++;
currentFrame %= shippics.Length;
RenderShip();
}
private void RenderShip()
var imageBrush = new ImageBrush();
imageBrush.Stretch = Stretch.None;
imageBrush.AlignmentX = AlignmentX.Left;
imageBrush.AlignmentY = AlignmentY.Top;
imageBrush.ImageSource = new BitmapImage(new Uri("/slShipAnimate;component/" + shippics[currentFrame], UriKind.Relative));
myShip.Fill = imageBrush;