[SOLVED] Tweenable Properties and OnComplete function?

I have a question about Tweening. I tried to tween a Rectangle and ran into problems, and was forced to tween a Vector2 instead like in the demo, and then on my Update function applied the Vector2 into the Rectangle I was trying to tween.

Can Rectangles be tweened, and if so, is there an example of how to tween a Rectangle?

Also, I looked at the Tween class and can’t find a way to call a function when my tween is finished. I can think of a workaround, like checking to see if the tween is active during an update function, and then calling a function when this returns false, but there has to be a more efficient way to do this.

Can a function be called at the end of a Tween?

Ok I figured out how to call a function at the end of a tween. This is how I did it:

public Action <Tween> notifyAnimationComplete;

public void OnCompleteFunc(Tween tween)
        {
            Console.Write("Tween Complete");
        }

 notifyAnimationComplete = OnCompleteFunc;

_tweener.TweenTo(this, a => a.myPoint, new Vector2(endRect.Width, endRect.Height), duration: duration, delay: delay)
                .Easing(EasingFunctions.SineOut)
                .OnEnd(notifyAnimationComplete);

Sorry to you veterans. I’m new to Actions. Man, this stuff should be in the docs!

Yeah, Action’s can be a little confusing when you’re new to them. They are very powerful when you get used to the idea though.

For example, there’s a few other ways you can write the same thing you have above.

  • You don’t need to store the Action in a variable. You can just pass the method directly into OnEnd like this:
_tweener.TweenTo(this, a => a.myPoint, new Vector2(endRect.Width, endRect.Height), duration: duration, delay: delay)
                .Easing(EasingFunctions.SineOut)
                .OnEnd(OnCompleteFunc);
  • Alternately, you can just inline the function directly using lambda syntax.
_tweener.TweenTo(this, a => a.myPoint, new Vector2(endRect.Width, endRect.Height), duration: duration, delay: delay)
                .Easing(EasingFunctions.SineOut)
                .OnEnd(tween => Console.Write("Tween Complete"));

or if you want to write more than one line of code:

_tweener.TweenTo(this, a => a.myPoint, new Vector2(endRect.Width, endRect.Height), duration: duration, delay: delay)
                .Easing(EasingFunctions.SineOut)
                .OnEnd(tween => 
                {
                    Console.Write("Tween Complete");
                    // do some other things here
                });

Once you’ve done it a few times you’ll wonder how you ever lived without them :wink:

1 Like