Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

phaser animation repeat event

preload ()
    {
        this.load.image('poo', 'assets/sprites/poo.png');
        this.load.spritesheet('mummy', 'assets/animations/mummy37x45.png', { frameWidth: 37, frameHeight: 45 });
    }

    create ()
    {
        const mummyAnimation = this.anims.create({
            key: 'walk',
            frames: this.anims.generateFrameNumbers('mummy'),
            frameRate: 16
        });

        const sprite = this.add.sprite(50, 300, 'mummy').setScale(4);

        sprite.play({ key: 'walk', repeat: 7 });

        this.tweens.add({
            targets: sprite,
            x: 750,
            duration: 8800,
            ease: 'Linear'
        });

        sprite.on('animationrepeat', function () {

            const poop = this.add.image(sprite.x - 32, 300, 'poo').setScale(0.5);

            this.tweens.add({
                targets: poop,
                props: {
                    x: {
                        value: '-=64', ease: 'Power1'
                    },
                    y: {
                        value: '+=50', ease: 'Bounce.easeOut'
                    }
                },
                duration: 750
            });

        }, this);
    }
Comment

phaser animation on repeat 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 (var i = 0; i < 13; i++)
        {
            this.add.image(64 * i, 536, 'tiles', 1).setOrigin(0);
        }

        this.add.image(576, 472, 'tiles', 27).setOrigin(0);
        this.add.image(576, 408, 'tiles', 27).setOrigin(0);
        this.add.image(576, 344, 'tiles', 57).setOrigin(0);

        this.add.text(400, 8, 'Click to repeat animation', { color: '#ffffff' }).setOrigin(0.5, 0);

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

        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 completes on our sprite
        lancelot.on(Phaser.Animations.Events.ANIMATION_REPEAT, function () {

            this.releaseItem();

        }, this);

        //  And a click handler to start the animation playing
        this.input.once('pointerdown', function () {

            lancelot.play('attack');

        });
    }

    releaseItem ()
    {
        const item = this.add.sprite(600, 370).play('coin');

        this.tweens.add({
            targets: item,
            props: {
                y: {
                    value: -64,
                    ease: 'Linear',
                    duration: 3000,
                },
                x: {
                    value: '+=128',
                    ease: 'Sine.inOut',
                    duration: 500,
                    yoyo: true,
                    repeat: 4
                }
            },
            onComplete: function () {
                item.destroy();
            }
        });
    }
Comment

PREVIOUS NEXT
Code Example
Javascript :: phaser pause all animations 
Javascript :: phaser animation random delay 
Javascript :: phaser stagger play 2 
Javascript :: react-native-fbsdk-next 
Javascript :: Horizontal scroll to anchor 
Javascript :: Who likes it 
Javascript :: Clean way to remove text and keep div inside a div jquery 
Javascript :: nodejs where multiple condition findAll 
Javascript :: Expresiones regulares para diferentes tipos de campos de formularios 
Javascript :: javascript 2021 
Javascript :: javascript to jquery code converter online 
Javascript :: HSETNX in redis 
Javascript :: nodelist example 
Javascript :: reduce function javascript 
Javascript :: javascript check item is checkbox 
Javascript :: do while loop js 
Javascript :: add to map javascript 
Javascript :: input variable in string javascript 
Javascript :: how to add dropdown with filter in angular material 
Javascript :: multer 
Javascript :: Lazy Loading 
Javascript :: javascript remove the last element from array 
Javascript :: how to read excel file in nodejs 
Javascript :: js code 
Javascript :: working of a recursive function 
Javascript :: javascript scrape page 
Javascript :: js get smallest value of array 
Javascript :: julia function 
Javascript :: javaScript add() Method 
Javascript :: how does an if statement work 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =