Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

phaser create animation on sprite

preload ()
    {
        this.load.atlas('soldier', 'assets/animations/soldier.png', 'assets/animations/soldier.json');
        this.load.image('bg', 'assets/pics/town-wreck.jpg');
    }

    create ()
    {
        this.add.image(400, 300, 'bg');

        const rambo = this.add.sprite(500, 500, 'soldier');

        //  The following animation is created directly on the 'rambo' Sprite.

        //  It cannot be used by any other sprite, and the key ('walk') is never added to
        //  the global Animation Manager, as it's kept local to this Sprite.
        rambo.anims.create({
            key: 'walk',
            frames: this.anims.generateFrameNames('soldier', { prefix: 'soldier_3_walk_', start: 1, end: 8 }),
            frameRate: 12,
            repeat: -1
        });

        //  Now let's create a new 'walk' animation that is stored in the global Animation Manager:
        this.anims.create({
            key: 'walk',
            frames: this.anims.generateFrameNames('soldier', { prefix: 'Soldier_2_walk_', start: 1, end: 8 }),
            frameRate: 12,
            repeat: -1
        });

        //  Because the rambo Sprite has its own 'walk' animation, it will play it:
        rambo.play('walk');

        //  However, this Sprite will play the global 'walk' animation, because it doesn't have its own:
        this.add.sprite(200, 500, 'soldier')
            .play('walk');
    }
Comment

phaser sprite animation event

  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');
            }

        });
    }
Comment

PREVIOUS NEXT
Code Example
Javascript :: phaser animation yoyo 
Javascript :: get lat long react native 
Javascript :: share.sharesingle facebook react native 
Javascript :: Node.js technical interview samples 
Javascript :: using cron with bull node js 
Javascript :: on refresh action set position rainmeter 
Javascript :: scan token deploy js 
Javascript :: js undici fetch data async 
::  
Javascript :: find numeric Unicode value of the character with charCodeAt() method 
Javascript :: nestjs TS2339 
Javascript :: mogoose schema to add json as a property 
Javascript :: express-js 
Javascript :: format numbers js 
Javascript :: empty array javascript 
Javascript :: js brightness 
Javascript :: useselector 
Javascript :: nextjs app 
Javascript :: mongoose remove 
Javascript :: how to do subtraction in javascript 
Javascript :: javascript callback function 
Javascript :: react-native spinner 
Javascript :: how to unfreeze object in javascript 
Javascript :: javascript exeit from loop 
Javascript :: how to remove an element from an array javascript 
Javascript :: Adding an item to an array 
Javascript :: how to display message in javascript 
Javascript :: get sessionstorage value in jquery 
Javascript :: react.createref 
Javascript :: how to name a file path in document.geteleementbyid 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =