How do I implement movement in a WPF Adventure game?

Posted by ZeroPhase on Game Development See other posts from Game Development or by ZeroPhase
Published on 2013-04-20T14:07:32Z Indexed on 2013/06/25 4:30 UTC
Read the original article Hit count: 268

Filed under:
|
|

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: http://i1118.photobucket.com/albums/k608/sealclubberr/clickToMove_zps9d4a33cc.png

ScreenShot with example movement: http://i1118.photobucket.com/albums/k608/sealclubberr/clickToMove_zps51f2359f.jpg

© Game Development or respective owner

Related posts about c#

Related posts about movement