Phaser:如何为 this.image.load 设置回退

Phaser: How to set a fallback for this.image.load

我正在从 URL 获取图像,有时会失败并显示以下消息。

GET https://the/image/url::ERR_HTTP2_PROTOCOL_ERROR

这不是 Phaser 造成的(虽然我不知道确切原因。这可以通过重新获取文件来解决。),但由于我只是使用 Phaser,我想在发生这种情况时向 Phaser 的负载添加回退 API。

下面是我用来加载图片的 API。

this.load.image('image_key', 'https://the/image/url')

document所说,我们可以在async等第三个参数中加上xhrSetting。

this.load.image('image_key', 'https://the/image/url'), {async: true}`

但是,我需要一个回退来处理我的抓取问题(失败时重新加载page/re-fetch文件,否则图像将是一个绿色框。),有什么办法可以实现吗?

通过向 loaderror 事件添加侦听器,您可以找到失败的文件

function preload ()
{
    this.load.on('loaderror', (reqinfo)=>{
        console.log(reqinfo);
    })
}

您可以找到更多详细信息here