我想知道是否可以在Phaser的给定时间动态加载资源,而不是在预加载函数中加载所有内容.原因很简单:我有一个有三个不同级别的游戏,所有游戏都有不同的背景歌曲; 所以我宁愿只在启动时加载一首歌来减少加载时间.
现在,我的预加载功能如下所示:
preload: function() { game.load.audio('pixel_world', ['assets/music/pixel_world_lo.ogg', 'assets/music/pixel_world_lo.mp3']); game.load.audio('second_source', ['assets/music/second_source_lo.ogg', 'assets/music/second_source_lo.mp3']); game.load.audio('reboot_complete', ['assets/music/reboot_complete_lo.ogg', 'assets/music/reboot_complete_lo.mp3']); game.load.image('pickup', 'assets/img/pickup.png'); }
我尝试将game.load.audio()之一调用移动到create函数:
create: function() { game.load.audio('pixel_world', ['assets/music/pixel_world_lo.ogg', 'assets/music/pixel_world_lo.mp3']); // good things follow... }
但是,以下调用失败:
this.cache.isSoundDecoded(level.song) // Phaser.Cache.isSoundDecoded: Key "pixel_world" not found in Cache. song = game.add.audio(level.song); // Phaser.Cache.getSound: Key "pixel_world" not found in Cache.
你知道如何让这个工作,或任何其他方式来确保在游戏启动时没有加载这三首歌曲?谢谢!
从文档中,对于像我这样的新手来说,那个大的未知:
audio(key,urls,autoDecode)→{Phaser.Loader}
将音频文件添加到当前加载队列.
调用此方法后,不会立即加载该文件.该文件将添加到队列中,以便在加载程序启动时加载.
所以基本上,preload之后的game.load.audio()没有加载歌曲,只是将它添加到队列中以供日后使用.为了加载歌曲,我还需要调用game.load.start():
create: function() { game.load.audio('pixel_world', ['assets/music/pixel_world_lo.ogg', 'assets/music/pixel_world_lo.mp3']); game.load.start(); // THIS! // good things follow... }