rexrainbow / phaser3-rex-notes

Notes of phaser3 engine
MIT License
1.18k stars 260 forks source link

upgrade to 1.60.6,destroy container will crash. #387

Closed gzjayvan closed 9 months ago

gzjayvan commented 9 months ago

从 1.60.4 更新到 1.60.6,container 调用 destroy 会引发错误。 在调用 destroy 之前,执行 getAll() 获取所有子元素并逐一执行 destroy 后再 执行 container 的 destroy 可避免错误。

rexrainbow commented 9 months ago

Test code with latest minify file. Works fine without any error message. Could you provide a simplest runnable test code to reproduce your case?

gzjayvan commented 9 months ago

My project uses TypeScript and Vite. When a container contains a Sizer or DropdownList (maybe other components ?), an error is triggered. but phaser3-rex-plugins@1.60.4 works fine.

error message:

Uncaught TypeError: Cannot read properties of undefined (reading 'sys')
    at Sizer.addToDisplayList (phaser.js:35008:67)
    at Container2.removeHandler (phaser.js:47829:24)
    at Object.Remove (phaser.js:238780:26)
    at Container2.remove (phaser.js:48237:34)
    at Sizer.RemoveFromContainer (P3Container.js:29:17)
    at Sizer.removeFromContainer (P3Container.js:68:29)
    at set parentContainer [as parentContainer] (ContainerLite.js:244:18)
    at Sizer.destroy (phaser.js:35205:14)
    at Sizer.destroy (Base.js:48:15)
    at Sizer.destroy (ContainerLite.js:35:15)

package.json:

{
  "main": "index.ts",
  "license": "UNLICENSED",
  "scripts": {
    "dev": "vite --host"
  },
  "devDependencies": {
    "phaser": "^3.60.0",
    "phaser3-rex-plugins": "^1.60.6",
    "typescript": "^5.2.2",
    "vite": "^4.4.11"
  }
}

code:

import Phaser from 'phaser';
import Label from 'phaser3-rex-plugins/templates/ui/label/Label';
import Sizer from 'phaser3-rex-plugins/templates/ui/sizer/Sizer';

class MainScene extends Phaser.Scene {
    constructor() {
        super('main_scene');
    }

    create() {
        const container = this.add.container(400, 300);
        container.add(this.create_sizer());
        container.destroy();
    }
    private create_sizer() {
        const sizer = new Sizer(
            this,
            {
                x: 0, y: 0,
                space: { item: 10 },
                orientation: 'x',
            }
        );
        for (var i = 0; i < 8; i++) {
            sizer.add(new Label(
                this, {
                x: 200, y: 200,
                width: 40, height: 40,
                text: this.add.text(0, 0, `${i}`),
                space: {
                    left: 10,
                    right: 10,
                    top: 10,
                    bottom: 10,
                },
                align: 'center'
            }));
        }
        sizer.layout();
        return sizer;
    }
}

const default_config: Phaser.Types.Core.GameConfig = {
    type: Phaser.AUTO,
    scale: {
        mode: Phaser.Scale.FIT,
        autoCenter: Phaser.Scale.CENTER_HORIZONTALLY,
        width: 800,
        height: 600,
        parent: `game`
    },
    disableContextMenu: true,
    scene: [
        MainScene
    ]
}

new Phaser.Game(default_config);
rexrainbow commented 9 months ago

This bug is fixed (commit). Please get latest minify file. NPM package will upgrade at end of this month.