[0]onEnable方法的执行顺序问题

场景结构
Image类型组件A ---->上面挂有脚本A
           |
           ------->Image类型组件B---->上面挂有脚本B
 
场景里有两个ImageA和B, B是A的子集,  两个Image上都挂有脚本
 
脚本执行顺序
A.onAwake ---> A.onEnable ---> B.onAwake ---> B.onEnable
 
通常编程逻辑 大概率是在父级中去调用子集   很少有子集去调用父级的
比如父级获得一个数据, 父级根据数据去设置子集属性
但是按照现在的初始化顺序  我在A.onEnable方法中去设置子集数据,但是子集onEnable方法还没有调用.导致子集访问某些属性会报空
 
所以现在这种方法执行顺序是否有些不合理,或者有没有方法控制onEnable的执行顺序
已邀请:

小高

赞同来自:

您是想实现什么需求呢?不是很能理解,但是在A里是可以修改B的属性的
4450.png


4532.png

178*****223

赞同来自:


import regClass = Laya.regClass;
import property = Laya.property;
import {B} from "./B";

@regClass()
export class A extends Laya.Script
{
@property({type:B})
public bbb:B;

public onEnable ():void
{
super.onEnable();
for(let i:number = 0; i < Math.floor(Math.random()*5); i++)
{
this.bbb.Move();
}
this.bbb.Restore();
}
}


import regClass = Laya.regClass;
import property = Laya.property;

@regClass()
export class B extends Laya.Script
{
@property({type:Laya.Image})
public image:Laya.Image;

public position:number;

public onEnable ():void
{
super.onEnable();
this.position = this.image.x;
}
public Move ():void
{
this.image.x += 500;
}
public Restore ():void
{
this.image.x = this.position;
}
}

 
A和B两个脚本
A挂在父级  B挂在子集
需要在onEnable时运行
应该先运行子集B的onEnable对子集做好初始化
然后再运行父级A的onEnable对子集应用

要回复问题请先

商务合作
商务合作