A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: getParentByName, just because

  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.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    And for those of you who hate repetition, here's an abstracted flexible version:
    Code:
    public static function getAncestorByFunction(d:DisplayObject, f:Function):DisplayObject{
      var p:DisplayObject = d;
      while ((p != null) && !(f(p))){
          p = d.parent;
      }
      return p;
    }
    Code:
    public static function getAncestorByName(d:DisplayObject, n:String):DisplayObject{
      return getAncestorByFunction(d, function(p:DisplayObject):Boolean{ return p.name == n; });
    }
    Last edited by 5TonsOfFlax; 05-15-2008 at 01:53 PM.

  3. #3
    Member
    Join Date
    Sep 2007
    Posts
    65
    Very nice.

  4. #4
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,136
    Um...it's interesting...
    the place where I'd see this having utility is in some piece of code that's adding further implementations of itself, or a loop between A adding B adding A etc., where no item knows how far up the ladder the initial parent was...
    for all that though, it would be a lot less memory waste and a lot faster to just pass the ancestor reference in question through all of those iterations, than to name every single one and go through the string comparisons like that...

    Far out, though. Nice to see you guys y'know, applying yourself and doing something useful; unlike most the fools around here...

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Yeah, this is not code that I would personally find useful generally. I would generally organize things with explicit references like you suggested.
    Writing it was an interesting exercise, and it seems like it could be a nice stepping stone for those that are having difficulty making the transition from displayList organized code to logically organized code.

  6. #6
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,136
    I wasn't knockin' it. You should see some of the ugly parent.parent["dynamicFunction"]() stuff I've had to write when I try to call code around applicationDomains... I probably coulda used something like this more than once...

    Just to crossthread for a sec...do you have any thoughts about the simple/impossible post I put up, uh, removing keys from an object...? I'm worried it's gonna flame out and...I'm wasted but I'm still coding after a 16-hour sprint so, any thoughts from your wisdomness would be of great gravity...

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