[LayaAir 2.0]Node.prototype._activeHierarchy存在的bug或者说是onEnable和onAwake的坑

官方源码如下:
/**
    *@private
    */
    __proto._activeHierarchy=function(activeChangeScripts){
        this._setBit(/*laya.Const.ACTIVE_INHIERARCHY*/0x02,true);
        if (this._components){
            for (var i=0,n=this._components.length;i < n;i++){
                var comp=this._components[i];
                comp._setActive(true);
                (comp._isScript())&& (activeChangeScripts.push(comp));
            }
        }
        this._onActive();
        for (i=0,n=this._children.length;i < n;i++){
            var child=this._children[i];
            (!child._getBit(/*laya.Const.NOT_ACTIVE*/0x01))&& (child._activeHierarchy(activeChangeScripts));
        }
        if (!this._getBit(/*laya.Const.AWAKED*/0x04)){
            this._setBit(/*laya.Const.AWAKED*/0x04,true);
            this.onAwake();
        }
        this.onEnable();
    }
出现问题原因:假如子控件在onEnable或者onAwake里调用removeSelf()(其它能从父控件移除的方法也一样)时,那么该函数就会报错,报错位置就是for循环里的child是undefined,不包含_getBit方法,出错原理是子控件从父控件移除会改变_children的长度,也就是n的值大于当前_children的长度。
修改如下:
var cs = this._children.concat();
        for (i=0,n=cs.length;i < n;i++){
            var child=cs[i];
            (!child._getBit(/*laya.Const.NOT_ACTIVE*/0x01))&& (child._activeHierarchy(activeChangeScripts));
        }
如果官方觉得这个不算bug的话,那么也就是说onEnable和onAwake里面勿执行从父控件移除的操作(延迟执行是没问题的)
微信图片_20190807104137.png
已邀请:

Aar0n

赞同来自:

这个是老版本引擎吧,是新版的引擎吗? 有没有复现demo

要回复问题请先

商务合作
商务合作