Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

phaser add animation event

preload ()
    {
        this.load.atlas('gems', 'assets/tests/columns/gems.png', 'assets/tests/columns/gems.json');
        // Local variable
        this.y = 160;
    }

    create ()
    {
        this.add.text(400, 32, 'Click to create animations', { color: '#00ff00' })
            .setOrigin(0.5, 0);

        //  Each time a new animation is added to the Animation Manager we'll call this function
        this.anims.on(Phaser.Animations.Events.ADD_ANIMATION, this.addAnimation, this);

        this.i = 0;

        //  Click to add an animation
        this.input.on('pointerup', function () {
            switch (this.i)
            {
                case 0:
                    this.anims.create({ key: 'diamond', frames: this.anims.generateFrameNames('gems', { prefix: 'diamond_', end: 15, zeroPad: 4 }), repeat: -1 });
                    break;

                case 1:
                    this.anims.create({ key: 'prism', frames: this.anims.generateFrameNames('gems', { prefix: 'prism_', end: 6, zeroPad: 4 }), repeat: -1 });
                    break;

                case 2:
                    this.anims.create({ key: 'ruby', frames: this.anims.generateFrameNames('gems', { prefix: 'ruby_', end: 6, zeroPad: 4 }), repeat: -1 });
                    break;

                case 3:
                    this.anims.create({ key: 'square', frames: this.anims.generateFrameNames('gems', { prefix: 'square_', end: 14, zeroPad: 4 }), repeat: -1 });
                    break;
            }
            this.i++;
        }, this);
    }

    addAnimation (key)
    {
        this.add.sprite(400, this.y, 'gems')
            .play(key);
        this.y += 100;
    }
Comment

phaser animation on update event

preload ()
    {
        this.load.atlas('knight', 'assets/animations/knight.png', 'assets/animations/knight.json');
        this.load.image('bg', 'assets/skies/clouds.png');
        this.load.spritesheet('tiles', 'assets/tilemaps/tiles/fantasy-tiles.png', { frameWidth: 64, frameHeight: 64 });
    }

    create ()
    {
        //  The background and floor
        this.add.image(400, 16, 'bg').setOrigin(0.5, 0);

        for (let i = 0; i < 13; i++)
        {
            this.add.image(64 * i, 536, 'tiles', 1)
                .setOrigin(0);
        }

        //  Our flowers
        for (let i = 0; i < 8; i++)
        {
            const flower = this.add.image(500, 472 - (i * 52), 'tiles', 31).setOrigin(0);
            this.flowers.push(flower);
        }

        this.add.text(400, 8, 'Click to play. Update Event on frame0004', { color: '#ffffff' })
            .setOrigin(0.5, 0);

        //  Our attack animation
        const attackConfig = {
            key: 'attack',
            frames: this.anims.generateFrameNames('knight', { prefix: 'attack_B/frame', start: 0, end: 12, zeroPad: 4 }),
            frameRate: 16
        };

        this.anims.create(attackConfig);

        //  Our coin animation
        const coinConfig = {
            key: 'coin',
            frames: this.anims.generateFrameNumbers('tiles', { start: 42, end: 47 }),
            frameRate: 12,
            repeat: -1
        };
        this.anims.create(coinConfig);

        const lancelot = this.add.sprite(300, 536, 'knight', 'attack_C/frame0000')

        lancelot.setOrigin(0.5, 1);
        lancelot.setScale(8);

        //  Event handler for when the animation updates on our sprite
        lancelot.on(Phaser.Animations.Events.ANIMATION_UPDATE, function (anim, frame, sprite, frameKey) {
            //  We can run our effect when we get frame0004:
            if (frameKey === 'attack_B/frame0004')
            {
                this.releaseItem();
            }

        }, this);

        //  And a click handler to start the animation playing
        this.input.on('pointerdown', function () {
            lancelot.play('attack', true);
        });
    }

    releaseItem ()
    {
        if (this.flowers.length === 0)
        {
            return;
        }

        const flower = this.flowers.pop();
        this.tweens.add({
            targets: flower,
            x: 864,
            ease: 'Quad.out',
            duration: 500
        });
    }
Comment

Animation Events Phaser 3

class Example extends Phaser.Scene
{
    constructor ()
    {
        super();
    }

    preload ()
    {
        this.load.atlas('sf2', 'assets/animations/sf2.png', 'assets/animations/sf2.json');
    }

    create ()
    {
        var animConfig = {
            key: 'ryu',
            frames: this.anims.generateFrameNames('sf2', { prefix: 'frame_', end: 22 }),
            frameRate: 20,
            repeat: 3
        };

        this.anims.create(animConfig);

        const sprite = this.add.sprite(550, 600, 'sf2', 'frame_0')
            .setOrigin(0.5, 1)
            .setScale(2);

        const text = this.add.text(32, 32, 'Click to Start Animation', { color: '#00ff00' });

        let log = [];
        let u = 0;
        let ui = 0;

        sprite.on(Phaser.Animations.Events.ANIMATION_START, function (anim, frame, gameObject) {

            log.push('ANIMATION_START');
            text.setText(log);

            u = 0;
            ui = 0;

        });

        sprite.on(Phaser.Animations.Events.ANIMATION_STOP, function (anim, frame, gameObject) {

            log.push('ANIMATION_STOP');
            text.setText(log);

            u = 0;
            ui = 0;

        });

        sprite.on(Phaser.Animations.Events.ANIMATION_UPDATE, function (anim, frame, gameObject) {

            if (u === 0)
            {
                log.push('ANIMATION_UPDATE x0');

                u++;
                ui = log.length - 1;
            }
            else
            {
                log[ui] = 'ANIMATION_UPDATE x' + u.toString();
                u++;
            }

            text.setText(log);

        });

        sprite.on(Phaser.Animations.Events.ANIMATION_REPEAT, function (anim, frame, gameObject) {

            u = 0;

            log.push('ANIMATION_REPEAT');

            text.setText(log);

        });

        sprite.on(Phaser.Animations.Events.ANIMATION_COMPLETE, function (anim, frame, gameObject) {

            log.push('ANIMATION_COMPLETE');

            text.setText(log);

        });

        this.input.on('pointerdown', function () {

            if (sprite.anims.isPlaying)
            {
                sprite.stop();
            }
            else
            {
                log = [];

                sprite.play('ryu');
            }

        });
    }
}

const config = {
    type: Phaser.AUTO,
    parent: 'phaser-example',
    width: 800,
    height: 600,
    pixelArt: true,
    scene: [ Example ]
};

const game = new Phaser.Game(config);
Comment

PREVIOUS NEXT
Code Example
Javascript :: phaser animation repeat event 
Javascript :: phaser create animation on sprite 
Javascript :: phaser hide animation on complete 
Javascript :: phaser pause all animations 
Javascript :: phaser stagger play 1 
Javascript :: add multiple phone using js 
Javascript :: NodeJS/express : Cached and 304 status code on chrome 
Javascript :: permissions in chrome extension javascript 
Javascript :: scan token test js 
Javascript :: Expresiones regulares para diferentes tipos de campos de formularios 
Javascript :: declare 2 d vector js 
Javascript :: site:stackoverflow.com two api calls dependent on each other js 
Javascript :: what is the syntax of putting an event listener in javascript mdn 
Javascript :: javascript detect if browser is not google chrome 
Javascript :: learn javascript 
Javascript :: javascript in jsx 
Javascript :: js filter example 
Javascript :: queryselector j 
Javascript :: create a class variable js 
Javascript :: js string 
Javascript :: react create context 
Javascript :: postgres json 
Javascript :: react native push notifications npm 
Javascript :: jsoup 
Javascript :: autofocus is not working in react native 
Javascript :: js module pattern 
Javascript :: sequelize find query to return raw data in json object format 
Javascript :: javascript callbacks 
Javascript :: discord.js command cooldown 
Javascript :: mongoose match aggregate 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =