【问题标题】:Only the first frame of the animation spritesheet plays仅播放动画 spritesheet 的第一帧
【发布时间】:2016-04-09 22:14:07
【问题描述】:

尝试在我的游戏中添加爆炸动画,但由于某种原因,只播放了精灵的第一帧。 Spritesheet 在预加载器中加载。

这是调用动画的函数

function asteroidCollisionHandler(player, asteroid){

live = lives.getFirstAlive();

if (live)
{
    live.kill();
}

explosion = explosions.getFirstExists(false);
explosion.reset(player.body.x, player.body.y);

explosion.play('explosion', 30, false, false);

if (lives.countLiving() < 1)
{
    player.kill();
}

}

创建爆炸组的函数

explosions = game.add.group();
explosions.createMultiple(30, 'explosion');

预加载器

this.load.spritesheet('explosion', 'images/explode.png', 128, 128, 16);

【问题讨论】:

    标签: javascript phaser-framework


    【解决方案1】:

    explosions.createMultiple() 行只创建了 30 个精灵,您仍然需要为每个精灵显式添加动画。您可以添加带有名称的动画,以及可选的帧等。顺便说一句,我建议为组和动画使用不同的名称以避免混淆,所以像这样:

    // initialise animations in the Create() function:
    for (var i = 0; i < grpexplosions.children.length; i++) {
        grpexplosions.children[i].animations.add('animexplode', [0,1,2,3], 30, true, false);
        //grpexplosions.children[i].animations.add('animexplode'); // alternatively, leave frames null to use all frames
    };
    
    // and then when you need one:
    explosion = grpexplosions.getFirstExists(false);
    explosion.play('animexplode', 30, false, false);
    

    顺便说一句,你也可以使用 group.callAll() 作为快捷方式而不是 for 循环,所以是这样的:

    var framesIndex = [0,1,2,3]; // or names
    grpexplosions.callAll('animations.add', 'animations', 'animexplode', framesIndex, 30, true, false);
    //grpexplosions.callAll('animations.add', 'animations', 'animexplode'); // frames optional, this will simply add all frames
    

    【讨论】:

      猜你喜欢
      • 2021-05-25
      • 1970-01-01
      • 2016-06-12
      • 2019-02-09
      • 1970-01-01
      • 1970-01-01
      • 2020-11-01
      • 2018-01-14
      • 2018-04-09
      相关资源
      最近更新 更多