I'm working on making a short WPF adventure game. The only major hurdle I have right now is how to animate objects on the screen correctly. I've experimented with DoubleAnimation
and ThicknessAnimation
both enable movement of the character, but the speed is a bit erratic. The objects I'm trying to move around are labels
in a grid
, I'm checking the mouse's position in terms of the canvas
I have the grid
in.
Does anyone have any suggestions for coding the movement, while still allowing mouse clicks to pick up items when needed? It would be nice if I could continue using the Visual Studio GUI Editor. By the way, I'm fine with scrapping labels
in a grid
for a more ideal object to manipulate.
Here's my movement code:
ThicknessAnimation ta = new ThicknessAnimation();
The event handling movement:
private void Hansel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
ta.FillBehavior = FillBehavior.HoldEnd;
ta.From = Hansel.Margin;
double newX = Mouse.GetPosition(PlayArea).X;
double newY = Mouse.GetPosition(PlayArea).Y;
if (newX < Convert.ToDouble(Hansel.Margin.Left))
{
//newX = -1 * newX;
ta.To = new Thickness(0, newY, newX, 0);
}
else if (newY < Convert.ToDouble(Hansel.Margin.Top))
{
newY = -1 * newY;
}
else
{
ta.To = new Thickness(newX, newY, 0, 0);
}
ta.Duration = new Duration(TimeSpan.FromSeconds(2));
Hansel.BeginAnimation(Grid.MarginProperty, ta);
}
Screenshot with annotations:
Screenshot with example movement: