简体   繁体   English

如何获取 Phaser 3 中加载过程的百分比信息?

[英]How do I get the percentage info of the loading process in Phaser 3?

I'm trying to add a loading animation for my phaser game, here is the code simulating the load process我正在尝试为我的移相器游戏添加加载 animation,这是模拟加载过程的代码

this.load.spritesheet('brawler', 'assets/animations/brawler48x48.png', {
  frameWidth: 48,
  frameHeight: 48
});
this.load.audio('goldrunner', 'assets/audio/Scyphe-Goldrunner_(Maccie_Pimp_Me Up_Remix).mp3');
this.load.image('taikodrummaster', 'assets/pics/taikodrummaster.jpg');
this.load.image('sukasuka-chtholly', 'assets/pics/sukasuka-chtholly.png');
this.load.video('chrome', 'assets/video/chrome.webm');

There are 5 assets in total.总共有5个资产。

I know I can use something like this.load.on('complete', function() {}) to check if the whole loading process is finished.我知道我可以使用类似this.load.on('complete', function() {})来检查整个加载过程是否完成。

I'd just like to know how do I get the percentage info of the loading process to give players a hint that how long the game would take to load the assets.我只想知道如何获取加载过程的百分比信息,以提示玩家游戏加载资产需要多长时间。

something like the following类似于以下内容

在此处输入图像描述

在此处输入图像描述

You can use the event progress , here is the link to the phaser documentation您可以使用事件progress ,这里是移相器文档的链接

Just add the line, in the preload methode of the Scene:只需在场景的preload方法中添加一行:

this.load.on('progress', function (progress) {
    // progress is a value between 0 and 1
    let percentage = parseInt(progress * 100);
    // Set here the label with the precentage
    ...
});    

Extra: If you want to track the progress of the file itself you could use the event fileprogress .额外:如果您想跟踪文件本身的进度,您可以使用事件fileprogress The callback of that specific event returns two parameters, on the file loaded and one with the percentage of the file that has been loaded.该特定事件的回调返回两个参数,一个是加载的文件,一个是已加载文件的百分比。
If you want a more fine grade result / information.如果您想要更精细的成绩/信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM