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 complete event

  {
        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.text(400, 8, 'Click to play animation', { color: '#ffffff' }).setOrigin(0.5, 0);

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

        this.anims.create(animConfig);

        const lancelot = this.add.sprite(400, 536, 'knight', 'attack_A/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_COMPLETE, function () {
            this.releaseItem();
        }, this);

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

    releaseItem() {
        const item = this.add.image(500, 500, 'tiles', 54);

        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

phaser animation on start 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.bg = this.add.tileSprite(0, 16, 800, 600, 'bg').setOrigin(0);
        this.ground = this.add.tileSprite(0, 536, 800, 64, 'tiles', 1).setOrigin(0);

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

        //  Our animations

        const idleConfig = {
            key: 'idle',
            frames: this.anims.generateFrameNames('knight', { prefix: 'idle/frame', start: 0, end: 5, zeroPad: 4 }),
            frameRate: 14,
            repeat: -1
        };

        this.anims.create(idleConfig);

        const runConfig = {
            key: 'run',
            frames: this.anims.generateFrameNames('knight', { prefix: 'run/frame', start: 0, end: 7, zeroPad: 4 }),
            frameRate: 12,
            repeat: -1
        };

        this.anims.create(runConfig);

        const lancelot = this.add.sprite(400, 536, 'knight');

        lancelot.setOrigin(0.5, 1);
        lancelot.setScale(8);
        lancelot.play('idle');

        //  Event handler for when the animation completes on our sprite
        lancelot.on(Phaser.Animations.Events.ANIMATION_START, function () {

            this.isRunning = true;

        }, this);

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

            lancelot.play('run');

        });
    }

    update ()
    {
        if (this.isRunning)
        {
            this.bg.tilePositionX += 4;
            this.ground.tilePositionX += 8;
        }
    }
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

PREVIOUS NEXT
Code Example
Javascript :: phaser play animation after repeat 
Javascript :: phaser show animation play through js 
Javascript :: School paperwork 
Javascript :: add multiple phone using js 
Javascript :: Node.js technical interview samples 
Javascript :: how to process string calculation in gdscript 
Javascript :: remove text in div JQuery zqqqqqqqqqqqqqqqzzzzzzzzzzzzzzzzzzzzzzzzzzzzzqqqqqqqqqqqqqqqqqq 
Javascript :: show data time &refresh 
Javascript :: node transitions 
Javascript :: JavaScript date format 2 
Javascript :: membuat validasi form dengan javascript 
Javascript :: white space below image next image 
Javascript :: js if animation infinity end 
Javascript :: expected a string (for built-in components) or a class/function (for composite components) but got: undefined 
Javascript :: how to declare a variable js 
Javascript :: json validate 
Javascript :: js print 
Javascript :: node js mongodb update nested object 
Javascript :: switch case 
Javascript :: type js 
Javascript :: what is diffrence between redux and context 
Javascript :: history of react js 
Javascript :: using for loops js 
Javascript :: js max number in array mdn 
Javascript :: js a function that takes in multiple arguments. 
Javascript :: white with opacity rgba to hex 
Javascript :: javascript code checker 
Javascript :: what is the meaning of the table innerhtml in javascript 
Javascript :: how to disable previous date in datepicker using angular 6 
Javascript :: js date toisostring with timezone 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =