Vilda
Vilda

Reputation: 1793

Pause phaser animation

I noticed that even though the

game.pause = true

pauses the game and stops the update cycle, all the animations continue to play. This is especially annoying in looped animations as it is more obvious.

Is there a way to pause all the running animations without explicitly keeping a list of them and pausing them 'manually'?

Upvotes: 0

Views: 1614

Answers (1)

Shohanur Rahaman
Shohanur Rahaman

Reputation: 387

To pause all animation you first add those animations in a group then pause the group. Here is a sample code pause all animation using group.

      var game = new Phaser.Game(800,600,Phaser.CANVAS,' ',{
    preload: preload, create: create
});


    function preload(){
        game.load.spritesheet('coin', 'assets/sprites/coin.png', 32, 32);
        // Note : load spritesheet without xml file

    }

var coins;
var flag = null;

    function create(){

        coins = game.add.group();

        for(var i=0;i<50;i++){
            coins.create(game.world.randomX,game.world.randomY,'coin',false);    
        }
        // NOTE : now using the power of callAll we can add same animation to all coins in the group
        coins.callAll('animations.add', 'animations', 'spin', [0,1,2,3,4,5], 10, true);
        // NOTE : the key should be 'animations' and the last param 'true' means repeatable

        coins.callAll('animations.play', 'animations', 'spin');

        var spaceKey = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
        spaceKey.onDown.add(pauseGame,this);
        flag = false;

    }

    function pauseGame(){
      if(flag == false){
            game.paused = true;
            flag = true;
        }
        else if(flag == true){
            game.paused = false;
            flag = false;
        }
    }

use spacebar to pause and play all the animations.

Upvotes: 1

Related Questions