Phaser在预加载后动态加载图像

时间:2016-06-13 00:46:14

标签: javascript image preload phaser-framework

对于Phaser,我正在开发一款游戏。该游戏奖励玩家不能预加载的物品。在Ajax调用之后,我希望加载图像,然后在移相器动画中显示它。反正有吗?

流程:游戏正在播放 游戏完成并进行Ajax调用。 Ajax响应使用哪个图像。 Phaser加载图像并显示他们赢得的内容。

2 个答案:

答案 0 :(得分:13)

您可以通过调用

随时使用Phaser的加载程序加载图像

game.load.image('referenceName', 'assets/pics/file.jpg');

然后您可以设置类似于的事件 http://phaser.io/examples/v2/loader/load-events

game.load.onLoadComplete.add(aFunctionToCall, this);

但最重要的是,不要忘记在设置好所有内容后告诉装载机。

game.load.start();

答案 1 :(得分:0)

Phaser 3 中的延迟加载可以通过使用 Phaser.Loader.LoaderPlugin(scene) 来完成

lazzyLoading:function(){
        var name = 'card-back';
        // texture needs to be loaded to create a placeholder card
        const card = this.add.image(WIDTH/2, HEIGHT/2, name);

        let loader = new Phaser.Loader.LoaderPlugin(this);
        // ask the LoaderPlugin to load the texture
        loader.image(name, 'assets/images/demon-large1.png');

        loader.once(Phaser.Loader.Events.COMPLETE, () => {
            // texture loaded so use instead of the placeholder
            card.setTexture(name)
        });
        loader.start();
    }
相关问题