Hello! To understand my question, it is worth remembering 2D games. We have the main character, who plows the map, practically always staying in the center of the screen. I have a picture as the main character. I have movement on the arrows. In fact, this is an animation that changes the coordinates of the image on the Canvas. I learned to move the ScrollViewer at the same time with the hero using svBar.ScrollToHorizontalOffset (svBar.ContentHorizontalOffset - 100) However, this happens jerky, but I would like to smoothly. Option, let the player move Skrollbar himself, in my opinion, not very good.

Also, I am ready to consider other ways out of this situation.

  • Why Scrollbar? Why not RenderTransform (or LayoutTransform)? And why always by 100, and not by the magnitude of the real displacement? - VladD 2:49 pm
  • Hmm, can you describe the idea in more detail? 100 is just a number that conveniently moved the screen for me. - Pinky
  • Well, every time a player moves due to the dynamics of the game, set the offset to (screen size / 2 is the player's position). - VladD
  • So, I figured out the offset using the RenderTransform and TranslateTransform. And what is the best way to make a player move in parallel with moving the screen? Provided that the player moves in small animated segments (if you hold down the movement in my case it will be unpleasant to twitch the screen). - Pinky
  • In general, I solved this question by writing an animation for TranslateTransform var a = new DoubleAnimation (); a.To = offset - 100; offset - = 100; a.Duration = TimeSpan.FromSeconds (0.25); cnField.RenderTransform.BeginAnimation (TranslateTransform.XProperty, a); - Pinky

1 answer 1

It seems to me that Scrollbar is not the best idea for this.) Nevertheless, it is better to move not by 100 at once, but in parts. For example:

Let PosX is the current position of the slider, NeedX - the one that it should get into. Vx - the speed of movement, then:

if ( (NeedX - PosX) > eps ){ PosX += Vx; } else if ( (PosX - NeedX) > eps){ PosX -= Vx; } 

This is all checked by timer) But, IMHO, it is better to add instead of Vx, Vx * dt? where dt is the time difference between frames, and update each frame)

I hope the course of my thoughts is clear)

  • This option was considered, yes, but I am afraid to produce extra timers. Animation, simply, works in its own, apparently, a separate thread, which allows you to run several at once. However, thanks anyway! - Pinky