Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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

phaser animation show on start

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

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

        var animConfig = {
            key: 'walk',
            frames: this.anims.generateFrameNames('bird', { prefix: 'frame', end: 9 }),
            repeat: -1,
            showOnStart: true
        };

        this.anims.create(animConfig);

        //  Create a bunch of random sprites
        const rect = new Phaser.Geom.Rectangle(64, 64, 672, 472);

        const group = this.add.group();
        group.createMultiple({ key: 'bird', frame: 0, quantity: 64, visible: false, active: false });

        //  Randomly position the sprites within the rectangle
        Phaser.Actions.RandomRectangle(group.getChildren(), rect);

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

            const bird = group.getFirstDead();

            if (bird)
            {
                bird.active = true;
                bird.setDepth(bird.y);

                //  As soon as we play the animation, the Sprite will be made visible
                bird.play('walk');
            }

        });
    }
Comment

PREVIOUS NEXT
Code Example
Javascript :: phaser pause animation instances 
Javascript :: phaser remove animation event 
Javascript :: phaser tween timescale 
Javascript :: javascript multiplication without operator 
Javascript :: chakra ui with humburger menu 
Javascript :: using cron with bull node js 
Javascript :: remove text and keep div inside a div jquery 
Javascript :: pass only second argument 
Javascript :: check letter case 
Javascript :: show json preformatted 
Javascript :: TypeError: (0 , import_dev.useParams) is not a function remix 
Javascript :: sadd in redis 
Javascript :: Adding A Function To All Node Example With Javascript 
Javascript :: postgresql nodejs 
Javascript :: javascript static methods 
Javascript :: js index of 
Javascript :: inertia.js 
Javascript :: convert html to javascript 
Javascript :: printing in javascript 
Javascript :: process node.js example 
Javascript :: javascript sort array of objects by key value ascending and descending order 
Javascript :: closure example 
Javascript :: javascript move element position 
Javascript :: sub function javascript 
Javascript :: Working of Recursion in C++ 
Javascript :: scarping js 
Javascript :: is there an api for netflix shows 
Javascript :: buffer image 
Javascript :: html get color gradient percentage 
Javascript :: electron install 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =