Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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 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 tween timescale 
Javascript :: accessing-nested-javascript-objects-and-arrays-by-string-path 
Javascript :: test unitaire javascript 
Javascript :: mui adding eye toggle at password field 
Javascript :: Who likes it 
Javascript :: _.isUndefined 
Javascript :: Opposites attract 
Javascript :: js undici fetch data with agent 
Javascript :: check notification permissopn allow or not 
Javascript :: react three fiber cannon collision 
Javascript :: mobile angular service 
Javascript :: the document has mutated since the result was returned 
Javascript :: decimal to hex 
Javascript :: compare text 
Javascript :: how to delete an element from an array in javascript 
Javascript :: javascript filter example 
Javascript :: npm read email 
Javascript :: react-scripts not found 
Javascript :: undefined value check in javascript 
Javascript :: react sass 
Javascript :: map && arrow function in javascript 
Javascript :: how to write to a file with javascript without nodejs 
Javascript :: object.assign in express 
Javascript :: console log all array values node 
Javascript :: react native loop in render 
Javascript :: javascript promise methods 
Javascript :: mdn javascript 
Javascript :: random password generator javascript 
Javascript :: where to create service angularor nodejs 
Javascript :: i get two times event click of button javascript 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =