|
-
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.
-
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.
-
-
Senior Member
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...
-
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.
-
Senior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|