creator 3.3.2 tiledMap.tmxAsset = null报错

  • Creator 版本: cocos creator 3.3.2

  • 目标平台: web

  • 重现方式:拖拽一个tiledmap,然后调用tmxAsset = null

  • 重现概率: 100%

您好,求助tiledmap的使用问题。

发现在调用this.tiledMap.tmxAsset = null;之后,会执行tiled-layer.ts的onDisable,从而抛出一个错误。

错误本身很奇怪,明明使用了【!.】语法过滤了null和undefined的情况,为何还会报错空指针的情况呢?

新建空项目测试结果一致,并且js版本的代码里,并没有过滤null和undefined。
image

辛苦引擎组大佬的回复,查了一下ts的语法,【parent!】是类型断言手动去除null和undefined,避免提示报错,【parent?】是非空才执行逻辑的简写。

  1. 想确认,this.tiledMap.tmxAsset = null;之后,执行到tiled-layer.ts的onDisable,抛出一个错误是否正常?
  2. decRef也尝试了下,也是一样的错误。如果想释放tmxAssets的正确方式是什么?

看了一下,这个问题是可以复现的,已经内部反馈了。感谢反馈。

另外,可以先使用 node.active = false 绕开 触发onDisable报错。

        this.map.node.active = false;
        console.log(this.map.node.active);

        this.map.tmxAsset = null;
        
        this.map.node.active = true;
        console.log(this.map.node.active);
1赞

感谢反馈。

  1. 想确认,this.tiledMap.tmxAsset = null;之后,执行到tiled-layer.ts的onDisable,抛出一个错误是否正常?
  • 这个不正常,这是一个BUG。
    问题原因: _releaseMapInfo 函数中会调用 layers[i].node.removeFromParent();,将node的parent关系移除。

会在最新的版本 3.4.2修复。

修复 PR https://github.com/cocos-creator/engine/pull/10062/files

好的,辛苦