[Amory]Haxe获取场景中的对象及调用其绑定的方法

通过对象名获取场景中的对象

var myObj = iron.Scene.active.getChild("the_object_name");

获取对象的某个child

    public static function getChild(obj: Object, childName:String) {
      if (obj == null) {
        return null;
      }
  
      if (obj.name!= childName) {
        return obj;
      }
  
      for (i in 0...obj.children.length) {
        var ch = getChild(obj.children[i],  childName);
        if (ch != null) {
            return ch;
        }
      }
      return null;
    } 

获取某个对象的rootParent

    public static function getParent(obj: Object) {
      if (obj == null) {
        return null;
      }
  
      if (obj.parent == null) {
        return obj;
      }
  
      return getParent(obj.parent);
    } 

调用对象绑定的某个方法

// 假设myObj绑定了SomeTrait类,类有dosomething()方法,
myObj.getTrait(SomeTrait).dosomething();
发布了382 篇原创文章 · 获赞 1048 · 访问量 244万+

猜你喜欢

转载自blog.csdn.net/moxiaomomo/article/details/104756460
今日推荐