Tween

Tweening is a technique that calculates transformations between start and end values. The way tweens are carried out depends on the tween's style and the time specified to reach the end value. In Slut, all aspects of such a tween are defined in a tween object and passed to a transformation (e.g: moveTo(), scaleBy()). Such a tween object is either instanced from one of Slut's tween classes or from any other freely defined tween class.

Slut currently defines the following tween classes:

  • Tween(x, y, z, duration)
  • SineTween(x, y, z, duration)

In some occasions one might want to cancel a tween before it has finished:

Relevant examples: tween.py , arrow_morph.py , ALL

In Context

spriteobject.moveBy(Tween(1.0, 0.0, 0.0, 1.5))
spriteobject.rotBy(Tween(0.0, 360.0, 0.0, 2.3))

Both of the above tween transformations utilize the standard Tween class which leads to a linear transformation. No acceleration or deceleration will occur. By using a different tween class this behavior can change.

spriteobject.moveBy(SineTween(1.0, 0.0, 0.0, 1.5))
spriteobject.rotBy(SineTween(0.0, 360.0, 0.0, 2.3))

Now the same two tweens will ease-in and ease-out according to a sine wave.

Since transformation and tween are separated, one can easily define new tweens that are applicable to all transformations. Creating a custom tween is a matter of defining a class that inherits from slut.element.Tween and overwrites the tweenfunc() method.

class RedundantLinearTween(Tween):
    def tweenfunc(self, change):
        return change/(self.duration*glo.fps)

Cancel methods are called on the element (sprite or tag) which exhibits the tween. To cancel all thrust movements of a sprite, for example, one would write the following line:

spriteobject.cancelTweenMoves()
Initiated by Stephan Hechenberger
Thanks to CADRE's 103