Adding tween callback to iteration example

Hi all, this is my first post but I’ve been using P2 for around a year and started using P3 about 2 months ago. So far I’m doing pretty well with the changes and find P3 to be a great improvement. I’ve been lurking around here and always found answers from searching the forum. However, I’ve been stumped by something that I’m hoping someone can help with…

I was hoping to use the checkerboard style reveal example but with the multiple tweens going on, for the life of me I can’t figure out how to implement a callback to know when the whole thing is complete.

The example I’m referring to is here: Checkerboard 4

var config = {
    type: Phaser.CANVAS,
    width: 800,
    height: 600,
    backgroundColor: '#080808',
    parent: 'phaser-example',
    scene: {
        preload: preload,
        create: create
    }
};

var game = new Phaser.Game(config);

function preload ()
{
    this.load.image('bg', 'assets/pics/skull-and-bones.jpg');
    this.load.image('block', 'assets/sprites/50x50-black.png');
}

function create ()
{
    this.add.image(400, 300, 'bg');

    var blocks = this.add.group({ key: 'block', repeat: 191 });

    Phaser.Actions.GridAlign(blocks.getChildren(), {
        width: 16,
        cellWidth: 50,
        cellHeight: 50,
        x: 25,
        y: 25
    });

    var _this = this;

    var i = 0;

    blocks.children.iterate(function (child) {

        _this.tweens.add({
            targets: child,
            scaleX: 0,
            scaleY: 0,
            alpha: 0,
            y: '+=64',
            angle: 180,
            ease: 'Power3',
            duration: 1000,
            delay: 1000 + (i * 100)
        });

        i++;

        //  Change the value 32 for different results
        if (i % 16 === 0)
        {
            i = 0;
        }

    });
}

Would appreciate any help. Thanks!

One way to do that is to add all the tweens to a timeline, each with offset: 0, then you can use the timeline’s onComplete callback.

But that example can actually be rewritten as one tween:

this.tweens.add({
  targets: blocks.getChildren(),
  scaleX: 0,
  scaleY: 0,
  alpha: 0,
  y: '+=64',
  angle: 180,
  ease: 'Power3',
  duration: 1000,
  delay: function(target, targetKey, value, targetIndex) {
    return 1000 + (targetIndex % 16) * 100;
  },
  onComplete: console.log
});
2 Likes

Of course! That makes perfect sense. Thank you for that. This will work perfectly. Much appreciated @samme