首页 > 解决方案 > 扩展 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)

标签: javascriptcontainersphaser-frameworkextending-classes

解决方案


推荐阅读