A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [RESOLVED] get all instances of a class?

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

    [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.

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    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. -

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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..

  4. #4
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,358
    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);

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

    resolved

    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center