【发布时间】:2015-08-22 18:01:49
【问题描述】:
我注意到即使
game.pause = true
暂停游戏并停止更新周期,所有动画继续播放。这在循环动画中尤其烦人,因为它更加明显。
有没有办法暂停所有正在运行的动画,而无需明确保留它们的列表并“手动”暂停它们?
【问题讨论】:
标签: javascript animation phaser-framework
我注意到即使
game.pause = true
暂停游戏并停止更新周期,所有动画继续播放。这在循环动画中尤其烦人,因为它更加明显。
有没有办法暂停所有正在运行的动画,而无需明确保留它们的列表并“手动”暂停它们?
【问题讨论】:
标签: javascript animation phaser-framework
要暂停所有动画,您首先将这些动画添加到组,然后暂停该组。这是使用 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;
}
}
使用空格键暂停和播放所有动画。
【讨论】: