Learn Phaser like Pro !

Beginner Intro to Tween in Phaser

Tweening is the process of interpolating between two values over time to create smooth animations. In Phaser, tweens are used to animate properties of game objects, such as position, scale, rotation, alpha, and more. you can even recreate physics simulation with tween.

You can create a tween using this.tweens.add() method. Here’s the basic example of tween:

var tween = this.tweens.add({
    targets: gameObject, // The game object to tween
    duration: duration, // Duration of the tween in milliseconds
    props: { property: value }, // Properties to tween
    ease: 'type', // Type of easing to use ('Linear', 'Cubic', 'Elastic', etc.)
    delay: delay, // Delay before the tween starts (in milliseconds)
    repeat: repeat, // Number of times to repeat the tween (-1 means repeat indefinitely)
    yoyo: yoyo // Whether the tween should return to its original state after completing
});
  1. targets : The targets property specifies the game object or array of game objects to tween.

  2. duration : The duration property specifies the duration of the tween in milliseconds.

  3. props : You can specify which properties of the game object(s) you want to tween using the props property. For example:

    props: {
        x: value,
        y: value,
        scaleX: value,
        rotation: value,
        alpha: value
    }
    
  4. ease : Easing determines how the animation progresses over time. Phaser provides various easing functions such as Linear, Quadratic, Cubic, Elastic, Bounce, etc. You can specify the easing function using the ease property.

  5. delay : The delay property specifies the delay before the tween starts (in milliseconds).

  6. repeat : The repeat property specifies the number of times the tween should repeat. Use -1 to repeat indefinitely.

  7. yoyo : The yoyo property specifies whether the tween should return to its original state after completing.

  8. Controlling Tweens : You can control tweens using methods like play, pause, resume, stop, etc., on the tween object returned by this.tweens.add().

Example: Here’s an example of a simple tween:

var tween = this.tweens.add({
    targets: gameObject,
    x: 400,
    y: 300,
    duration: 2000,
    ease: 'Linear',
    yoyo: true,
    repeat: -1
});

This tween will move gameObject to (400, 300) over 2 seconds, then return it to its original position, and repeat indefinitely.

On The next tutorials we will explore more advance concept of tweens.