Phaser预加载后动态加载图像

9

我正在使用Phaser开发一款游戏。在这个游戏中,玩家将获得一个无法预加载的物品。我希望在Ajax调用后加载图像,然后在Phaser动画中显示它。有没有什么方法可以实现这个?

流程:游戏进行中,游戏结束并发起Ajax调用,Ajax响应并告诉我们要使用哪个图片,Phaser加载该图片并显示玩家获得的奖品。

2个回答

21

您可以使用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();


这对我也起作用了。我调用了多次图像加载,似乎在调用onLoadComplete之前会等待它们全部完成。 - Manuel Hernandez

3
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();
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接