Hello. There is a function that moves an object one cell to the right. After animating the movement, you need to save the current position of the object. And the next time you call this function, you need to animate the movement already from the new position of the object.

Here is the function code

public void MoveRight(GameElement element) { TranslateTransform trans = new TranslateTransform(); element.RenderTransform = trans; DoubleAnimation anim1 = new DoubleAnimation(0, Constants.CellSize, TimeSpan.FromSeconds(1)); trans.BeginAnimation(TranslateTransform.XProperty, anim1); element.PositionX += Constants.CellSize; } 

But, the next time you call this function, the animation still starts from the very first point, despite the changed position of the object.

I tried this way:

 DoubleAnimation anim1 = new DoubleAnimation(element.PositionX, element.PositionX + Constants.CellSize, TimeSpan.FromSeconds(1)); 

But then when you first call the object jumps to the distance element.PositionX , then comes the animation.

Help me please

    1 answer 1

    Variant "In the forehead": Subscribe to the anim1.Completed event and upon actual triggering, set the current position values ​​for the object.

    More on the topic: The animation has properties / flags anim1.IsAdditive and anim1.IsCumulative . Judging by the description, they control the change in the parameters of the points of the beginning and end of the animation (as I understand the coordinates of the object itself do not change). Try to "play" with these flags, perhaps using them to implement the behavior you need.

    • one
      Maybe it's better the other way around? To “lead” an element to a new position, set the animation from the offset of the old position relative to the new one to zero, and lose? - VladD
    • @VladD As an option. I would even say more preferable so as not to produce extra code. This approach will allow you to stay within the framework of the MoveRight method. - Alexey