Created
May 19, 2013 06:12
-
-
Save aschearer/5606865 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Page | |
x:Class="SpottedZebra.Examples.WinRT.SpritesheetExamplePage" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
mc:Ignorable="d"> | |
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> | |
<Rectangle Width="100" Height="150"> | |
<Rectangle.Fill> | |
<ImageBrush ImageSource="/Images/OwlSpritesheet.png" | |
Stretch="None" | |
AlignmentX="Left" | |
AlignmentY="Top"> | |
<ImageBrush.Transform> | |
<TranslateTransform x:Name="SpriteSheetOffset" X="0" Y="0" /> | |
</ImageBrush.Transform> | |
</ImageBrush> | |
</Rectangle.Fill> | |
</Rectangle> | |
</Grid> | |
</Page> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace SpottedZebra.Examples.WinRT | |
{ | |
using System; | |
using Windows.UI.Xaml.Controls; | |
using Windows.UI.Xaml.Media; | |
using Windows.UI.Xaml.Navigation; | |
public sealed partial class SpritesheetExamplePage : Page | |
{ | |
private const int NumberOfColumns = 10; | |
private const int NumberOfFrames = 51; | |
private const int FrameWidth = 100; | |
private const int FrameHeight = 150; | |
public static readonly TimeSpan TimePerFrame = TimeSpan.FromSeconds(0.5); | |
private int currentFrame; | |
private TimeSpan timeTillNextFrame; | |
public SpritesheetExamplePage() | |
{ | |
this.InitializeComponent(); | |
} | |
protected override void OnNavigatedTo(NavigationEventArgs e) | |
{ | |
CompositionTarget.Rendering += this.OnUpdate; | |
} | |
protected override void OnNavigatedFrom(NavigationEventArgs e) | |
{ | |
CompositionTarget.Rendering -= this.OnUpdate; | |
} | |
private void OnUpdate(object sender, object e) | |
{ | |
this.timeTillNextFrame += TimeSpan.FromSeconds(1 / 60f); | |
if (this.timeTillNextFrame > TimePerFrame) | |
{ | |
this.currentFrame = (this.currentFrame + 1 + NumberOfFrames) % NumberOfFrames; | |
var column = this.currentFrame % NumberOfColumns; | |
var row = this.currentFrame / NumberOfColumns; | |
this.SpriteSheetOffset.X = -column * FrameWidth; | |
this.SpriteSheetOffset.Y = -row * FrameHeight; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@geoffles, thank you. I struggled with this for a whole day.