Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

phaser animation on stop 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 stop animation', { color: '#ffffff' }).setOrigin(0.5, 0);

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

        this.anims.create(animConfig);

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

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

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

            this.isRunning = false;

        }, this);

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

            lancelot.stop();

        });
    }

    update ()
    {
        if (this.isRunning)
        {
            this.bg.tilePositionX += 2;
            this.ground.tilePositionX += 4;
        }
    }
Comment

PREVIOUS NEXT
Code Example
Javascript :: phaser play animation after delay 
Javascript :: phaser reverse animation 
Javascript :: phaser animation yoyo 
Javascript :: append input using js 
Javascript :: mui adding eye toggle at password field 
Javascript :: javascript accordion 
Javascript :: remove text in div JQuery zqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq 
Javascript :: when end sound show alert 
Javascript :: angular reactive forms bootstrap 4 
Javascript :: declare 2 d vector js 
Javascript :: prevent alpine js from rendering components during refresh 
Javascript :: smembers in redis 
Javascript :: Using the forEach function In JavaScript 
Javascript :: how to print in html 
Javascript :: TypeError: expressValidator is not a function 
Javascript :: class declaration in javascript 
Javascript :: javascript basic programs 
Javascript :: what is local storage and session storage in javascript 
Javascript :: how to initialize an array in javascript 
Javascript :: get id javascript 
Javascript :: how to make a delete button in javascript 
Javascript :: javascript operators 
Javascript :: install tailwind css with next js 
Javascript :: sort an array 
Javascript :: javascrip functions parameters 
Javascript :: converter rgba to hex without opacity 
Javascript :: javascript foreach call specific value in array 
Javascript :: date range picker in angular 8 
Javascript :: arrow function syntax vs function expression syntax 
Javascript :: remove duplicate item from array javascript 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =