|
-
[RESOLVED] get all instances of a class?
Is it possible to get all instances of a class at runtime? I'm looking for something like javascript's getElementsByTagName, or an xpath request, or creating a treewalker (how I did it in AS2).
I'd like to get all TextField instances and apply some operations to them, including adding event handlers.
It seems impossible to set the baseclass for a textfield without first turning it into a Symbol.
This is in flash9, NOT flex.
-
Senior Member
If you give them similar names you can use a for loop with getChildByName("name"). First thing coming into my mind.
- The right of the People to create Flash movies shall not be infringed. -
-
Unfortunately, I will not know the names of the TextFields beforehand. This is code to be used in a supporting library, so the particulars of the calling movies are unknown..
-
half as fun, double the price
If they're all on screen, you could loop through the display list and find them
Code:
function findDisplayInstances(target:DisplayObjectContainer, type:Class):Array {
var found:Array = [];
var child:DisplayObject;
var i:int = target.numChildren;
while (i--) {
child = target.getChildAt(i);
if (child is type){
found.push(child);
}
if (child is DisplayObjectContainer){
found = found.concat(findDisplayInstances(DisplayObjectContainer(child), type));
}
}
return found;
}
var allTextFields:Array = findDisplayInstances(this, TextField);
-
I'm not sure I like it, but I've coded something similar. Perhaps more flexible: a general treeWalker function:
Code:
/**
* Recursively walks displayObjectContainer display list, applying doFunc to children that pass filterFunc
*
* @param container DisplayObjectContainer to traverse
* @param doF Function that takes a DisplayObject, and returns void.
* @param filterF Function that takes a DisplayObject, and returns a boolean. If true, doFunc is called on the DisplayObject
* @return void
*/
public static function walkTree(container:DisplayObjectContainer, doF:Function, filterF:Function):void{
var child:DisplayObject;
for (var i:uint=0; i < container.numChildren; i++){
child = container.getChildAt(i);
if (filterF(child)){
doF(child);
}
if (child is DisplayObjectContainer){
//DisplayObjectContainer(child) is actually casting. Could use 'as', but that doesn't throw a TypeError
walkTree(DisplayObjectContainer(child), doF, filterF);
}
}
}
I wanted to make filterF have a default value of function(o:Object){return true;}, but the compiler doesn't like it.
To then trace all TextFields, I did something like this:
Code:
function isTextField(o:Object):Boolean{
return (o is TextField);
}
Utils.walkTree(this, trace, isTextField);
Also, I'm not sure if this'll work for me because of the on-screen limitation, but I can probably deal with that.
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
|