根据 CHANGELOG ,自版本 3.53.0 起 - Anastasia - 2021 年 3 月 8 日
SceneManager.loadComplete
will no longer try to unlock the Sound Manager, preventingAudioContext was not allowed to start
console warnings after each Scene finishes loading.
但是,每次我在 Chrome 96.0.4664.110(官方版本)(x86_64) 上运行 Phaser3 应用时
var game = new Phaser.Game();
<script src="https://cdn.jsdelivr.net/npm/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0c0d8d1c3d5c2f0839e85859e82" rel="noreferrer noopener nofollow">[email protected]</a>/dist/phaser.js"></script>
我仍然收到那个烦人的警告。
我错过了什么吗?
最佳答案
问题是,这是浏览器“问题”(查看此链接,了解 chromium 示例: https://bugs.chromium.org/p/chromium/issues/detail?id=943258#c6 )
如果您在没有用户交互的情况下创建 AudioContext
,浏览器会输出此警告。 Phaser 在创建 Phaser 游戏对象 new Phaser.Game(...);
时创建一个 AudioContext
。
如果你想摆脱警告消息,我有两种不同的想法,你可以如何做到这一点:
选项 1:
制作一个带有开始按钮/图像/...的简单 html 页面,并且仅在用户迭代后创建 Phaser.Game
对象,那么就不会出现警告。 (这是“更干净”,有点开销)
let button = document.querySelector('#btnStart');
button.addEventListener('click', () => {
var game = new Phaser.Game();
});
<script src="https://cdn.jsdelivr.net/npm/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="92e2faf3e1f7e0d2a1bca7a7bca0" rel="noreferrer noopener nofollow">[email protected]</a>/dist/phaser.js"></script>
<button id="btnStart"> Start </button>
选项 2:
或者,您可以在创建对象后使用 console.clear();
清除控制台。 (简单快速修复)
var game = new Phaser.Game();
console.clear();
<script src="https://cdn.jsdelivr.net/npm/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9c9d1d8cadccbf98a978c8c978b" rel="noreferrer noopener nofollow">[email protected]</a>/dist/phaser.js"></script>
Just so that you know the Warning has no effect, a can be ignored.
关于javascript - 根据 CHANGELOG,由于版本 3.53.0 Phaser3 正在阻止 `AudioContext was not allowed` 警告,但我仍然遇到它,我该怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70633071/