You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Phaser 3.60.0音频缓存缺失求助:'backgroundMusic'键未找到

解决Phaser 3.60.0中音频缓存缺失的错误

以下是针对你遇到的问题的具体修复步骤:

  • 修复文件路径的转义错误
    JavaScript字符串中的反斜杠是转义字符,你当前写的路径.\music\backgroundMusic.mp4会被解析错误,导致音频资源根本没加载进缓存。将路径改为正斜杠写法:

    //preload function:
    this.load.audio('backgroundMusic', './music/backgroundMusic.mp4');
    

    或者使用双反斜杠(兼容Windows路径的写法):

    this.load.audio('backgroundMusic', '.\\music\\backgroundMusic.mp4');
    
  • 修正create函数中的变量引用问题
    你在create函数里声明了局部变量backgroundMusic,但后续却用this.backgroundMusic访问,这会触发额外的undefined错误(虽然当前主报错是缓存缺失,但这个问题也需要修复)。正确写法是将音频实例挂载到this上:

    //create function:
    this.backgroundMusic = this.sound.add('backgroundMusic');
    this.backgroundMusic.loop = true;
    this.backgroundMusic.play();
    
  • 可选优化:检查音频格式兼容性
    MP4作为音频容器在部分浏览器中兼容性不如MP3或OGG,建议换成标准音频格式(比如MP3),避免后续出现加载或播放异常。

内容的提问来源于stack exchange,提问作者AshBoomstick1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 05:14:57