CocosCreator addChild() 方法出现BUG

  • Creator 版本: 2.4.2

  • 目标平台: Chrome

  • 重现方式:项目中调用
    @property(cc.Node)
    node_001: cc.Node = null;
    @property(cc.Node)
    node_002: cc.Node = null;

    @property(cc.Label)
    label: cc.Label = null;

    @property
    text: string = ‘hello’;

    start () {
    // init logic
    this.label.string = this.text;

      this.node_001.addChild(this.node_002);
    

    }

  • 首个报错:
    Cocos Creator v2.4.2
    CCDebug.js:216 Uncaught Error: child already added. It can’t be added again
    at Object.cc.assert (CCDebug.js:149)
    at Object.101.cc.assertID (CCDebug.js:260)
    at cc_Node.addChild (CCNode.js:3655)
    at Helloworld.start (eval at (quick_compile.js:238), :51:23)
    at eval (eval at createInvokeImpl (component-scheduler.js:219), :3:65)
    at CCClass._invoke (component-scheduler.js:224)
    at CCClass.invoke (component-scheduler.js:140)
    at CCClass.startPhase (component-scheduler.js:480)
    at 102.cc.Director.mainLoop (CCDirector.js:887)
    at callback (CCGame.js:632)

  • 之前哪个版本是正常的:

  • 手机型号:

  • 手机浏览器:

  • 编辑器操作系统: win 7

  • 重现概率: 必定出现<a class=“attachment”

顶一下,感觉是个大问题

this.node_002已经有父节点了吧

有,但不是目标节点

Cocos Creator v2.4.2
CCDebug.js:216 Uncaught Error: child already added. It can’t be added again
at Object.cc.assert (CCDebug.js:149)
at Object.101.cc.assertID (CCDebug.js:260)
at cc_Node.addChild (CCNode.js:3655)
at Helloworld.start (eval at (quick_compile.js:238), :51:23)
at eval (eval at createInvokeImpl (component-scheduler.js:219), :3:65)
at CCClass._invoke (component-scheduler.js:224)
at CCClass.invoke (component-scheduler.js:140)
at CCClass.startPhase (component-scheduler.js:480)
at 102.cc.Director.mainLoop (CCDirector.js:887)
at callback (CCGame.js:632)

既然有了父节点就直接用cc.instance复制一个node_002,然后再用node_001来addChild

问题是之前错了

我有更新了
问题

是我要把node_002添加到node_001节点下,
this.node_001.addChild(this.node_002);

Uncaught Error: child already added. It can’t be added again
未捕获错误:已添加子元素。不能再加了

assets (1).rar (41.6 KB)

之前的版本我这样使用是正常的

这个节点已经存在了 肯定不能重复添加 你想实现你这样的效果 先克隆一个002. 然后addchild002 这个问题就可以解决啦.

这个不是bug啊,是node_002已经存在父节点了,你想改变父节点,就不能用addChild, 只能node_002.parent = node_001 或者setParent(node_001)

我相当于想给node_001换一个父级,但是不想使用parent设置

克隆肯定就不会有问题啊,单单克隆操作出来不会默认存在父节点的

你想给001换父级- - 你操作002干啥 为啥不想操作parent

对阿… 我和他说 克隆个新节点 然后addchild就会多一个节点出来 如果 想改变父级 就用你的方式就可以了

这样写就没事了谢谢!!!
this.node_002.parent = null;
this.node_001.addChild(this.node_002);

强迫症嘛:3: addChild方法底层还是用的设置节点的parent属性的

有一点,就一点点