javascript - 扩展 Phaser3 容器类抛出 TypeError
问题描述
我正在尝试扩展 phaser3 容器类以获取带有一些默认项目的可重用框。我读了这个。我尝试了不同的方法来传递场景对象,但都没有奏效。我也不确定这是否真的是问题所在。非常感谢任何帮助或提示。
var config = {
key: 'firstScene',
type: Phaser.AUTO,
width: 800,
height: 600,
physics: {
default: 'arcade',
arcade: {
gravity: { y: 300 },
debug: false
}
},
scene: {
preload: preload,
create: create,
update: update
}
};
var platforms;
var game = new Phaser.Game(config);
function preload ()
{
this.load.image('floor', 'assets/floor_1.png');
this.load.image('ceiling_bar', 'assets/ceiling_1.png');
this.load.image('sky', 'assets/sky.png');
this.load.image('ground', 'assets/platform.png');
this.load.image('star', 'assets/star.png');
this.load.image('bomb', 'assets/bomb.png');
this.load.spritesheet('dude', 'assets/dude.png', { frameWidth: 32, frameHeight: 48 });
this.load.image('bar', 'assets/bar_2.png');
}
var platforms;
class Dungeon extends Phaser.GameObjects.Container {
bottom_bar = this.add.image(x, y-20, 'floor');
ceiling_bar = this.add.image(x, y+20, 'ceiling_bar');
children = [ 'bottom_bar', 'ceiling_bar' ]
constructor(scene, x, y, children ) {
super(scene, x, y, children);
}
//scene.add.existing(this);
}
function create ()
{
this.add.image(400, 300, 'sky');
var ground_bar = this.add.image(200, 100, 'floor');
//var top_bar = this.add.image(100, 40, 'bar');
console.log(this.scene.scene);
var dungeon = this.add.existing(new Dungeon(this.scene.scene, 300, 400, ['top_bar']))
//var dungeon = this.add.existing(new Dungeon(this.scene, 300, 400, ['top_bar']))
//var dungeon = this.add.existing(new Dungeon(this, 300, 400, ['top_bar']))
//var dungeon = new Dungeon(this, 300, 400, ['top_bar']);
var conti = this.add.container(100, 200, ['floor']);
}
这不会显示“top_bar”图像,但会抛出
phaser.js:59938 Uncaught TypeError: gameObject.once is not a function
at Dungeon.addHandler (phaser.js:59938)
at Object.Add (phaser.js:104894)
at Dungeon.add (phaser.js:60039)
at new Container (phaser.js:59775)
at new Dungeon (main.js:43)
at Scene.create (main.js:55)
at SceneManager.create (phaser.js:46843)
at SceneManager.loadComplete (phaser.js:46737)
at LoaderPlugin.emit (phaser.js:2011)
at LoaderPlugin.loadComplete (phaser.js:89330)
解决方案
推荐阅读
- mapbox - 离线路由 Mapbox 数据集
- python - VSCode ModuleNotFoundError:没有名为 X 的模块
- spring-boot - 如何在不同端口上测试两个安全适配器
- javascript - 在 gulp 中遇到问题
- python - Python os.walk 用于没有驱动器号的未安装驱动器
- visual-foxpro - 在 VFP 中使用谷歌地图问题控制 Shell.Explorer.2
- java - 从伦敦的纬度和经度坐标算出 50 英里半径
- python - 带有命令的机器人不和谐 python 消息
- python - 通过 pip 安装 TensorFlow 的问题
- css - 材质 UI 表格额外填充