【问题标题】:Pause phaser animation暂停移相器动画
【发布时间】:2015-08-22 18:01:49
【问题描述】:

我注意到即使

game.pause = true

暂停游戏并停止更新周期,所有动画继续播放。这在循环动画中尤其烦人,因为它更加明显。

有没有办法暂停所有正在运行的动画,而无需明确保留它们的列表并“手动”暂停它们?

【问题讨论】:

    标签: javascript animation phaser-framework


    【解决方案1】:

    要暂停所有动画,您首先将这些动画添加到,然后暂停该组。这是使用 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;
            }
        }
    

    使用空格键暂停和播放所有动画。

    【讨论】:

    • 恐怕我还是不明白。为什么 game.paused 隐式影响组而不是单个精灵?在您的示例中没有暂停组的显式调用
    • 您可以使用“animations.paused = boolean”来暂停单个动画,但是当您想暂停所有动画时,您必须使用组。除了组之外,我没有发现任何影响一组动画的方法。
    猜你喜欢
    • 2016-01-09
    • 2021-07-11
    • 1970-01-01
    • 2016-03-28
    • 2013-11-25
    • 2012-08-21
    • 2015-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多