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

Phaser场景加载异常求助:浏览器仅显示黑色画布无文本

问题解决:Phaser游戏仅显示黑色画布且文本不渲染

问题根源及修复方案:

  • 背景色配置拼写错误:原代码中backgroundcColor少写了一个k,正确应为backgroundColor,这会导致背景色未生效,显示默认黑色画布。
  • 重复创建游戏实例:代码里先后两次创建Phaser.Game实例,无参创建的实例会干扰后续带配置的实例,需删除第一个无参的var game = new Phaser.Game();语句。
  • 脚本加载顺序错误:必须确保Scene1.js在game.js之前引入到HTML页面中,否则game.js执行时无法找到Scene1类,场景无法正常初始化。

修正后的代码:

game.js

window.onload=function(){
    var config = {
        width: 800,
        height: 500,
        backgroundColor: 0xffffff,
        scene: [Scene1, Scene2]
    }
    var game = new Phaser.Game(config);
}

Scene1.js

class Scene1 extends Phaser.Scene {
    constructor(){
        super("Scene1");
    }
    create(){
        this.add.text(20,20,"Loading game", {font:"25px Arial", fill:"yellow"});
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 08:20:41