A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: getParentByName, just because

Threaded View

  1. #1
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801

    getParentByName, just because

    In talking offline with simplexian, the idea of a getParentByName function came up. I figured it might be useful to many of us.

    Code:
    public static function getAncestorByName(d:DisplayObject, name:String):DisplayObject{
      var p:DisplayObject = d;
      while ((p != null) && (p.name != name)){
          p = d.parent;
      }
      return p;
    }
    And here's one that may be more useful, to get a parent by Class. This will return the first ancestor that IS an instance of clazz or anything extending it, but if you replace is with instanceof, it'll look for the exact matching class.

    Code:
    public static function getAncestorByClass(d:DisplayObject, clazz:Class):DisplayObject{
      var p:DisplayObject = d;
      while ((p != null) && !(p is clazz)){
          p = d.parent;
      }
      return p;
    }
    All code untested. Please give feedback.

    Edit: was confused about DisplayObjects and DisplayObjectContainers.
    Last edited by 5TonsOfFlax; 05-15-2008 at 01:48 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center