Progaramatically declare Type - Macromedia Flash

This is a discussion on Progaramatically declare Type - Macromedia Flash ; Hi There consider this recursive function: private function setChildrenEditable(DObj isplayObjectContainer,f:Function):void{ for(var i:int=0;i<DObj.numChildren;i++){ if (DObj.getChildAt(i) is IContent){ //do the function var c:IContent = DObj.getChildAt(i) as IContent; f(c); }else if(DObj.getChildAt(i) is DisplayObjectContainer){ //do the recursion setChildrenEditable(DisplayObjectContainer(DObj.getChildAt(i)),f); } } } it takes a ...

+ Reply to Thread
Results 1 to 3 of 3

Progaramatically declare Type

  1. Default Progaramatically declare Type

    Hi There

    consider this recursive function:

    private function
    setChildrenEditable(DObjisplayObjectContainer,f:Function):void{
    for(var i:int=0;i<DObj.numChildren;i++){
    if (DObj.getChildAt(i) is IContent){
    //do the function
    var c:IContent = DObj.getChildAt(i) as IContent;
    f(c);
    }else if(DObj.getChildAt(i) is DisplayObjectContainer){
    //do the recursion
    setChildrenEditable(DisplayObjectContainer(DObj.getChildAt(i)),f);
    }
    }
    }

    it takes a display object container and recursively sends all children
    of a specific type to function f. What I would like to do is to pass
    the type of child that the method looks for so instead of
    setChildrenEditable(DObjisplayObjectContainer,f:Function):void

    it said
    setChildrenEditable(DObjisplayObjectContainer,f:Function,Type:???):void

    I could the make it a nice static method for use in lots of different
    situations. Any ideas?


    Cheers Richard


  2. Default Re: Progaramatically declare Type

    This should be in the AS3 forum. However, here's something I use that pretty
    much does what you need.

    import flash.display.*;
    import flash.utils.*;

    function myCallback(target:Object)
    {
    trace("myCallback", target);
    }

    function findChildrenByType(containerisplayObjectContainer,
    childType:Object = null,
    callback:Function = null,
    recursive:Boolean=true
    ):Array
    {
    var result:Array = new Array();
    if (container==null){return result;}

    var typeClass:Class = (childType==null) ? DisplayObject :
    childType is Class ? childType as Class : null;

    if (typeClass == null)
    {
    var className:String = flash.utils.getQualifiedClassName(childType);
    typeClass = flash.utils.getDefinitionByName(className) as Class;
    }

    var objisplayObject;
    for(var i:uint=0; i<container.numChildren; i++)
    {
    obj = container.getChildAt(i);
    if(obj is typeClass)
    {
    result.push(obj);
    if (callback != null)
    {
    try{callback(obj);} // trap argument count mismatch
    catch(e){}
    }
    }
    if (obj is DisplayObjectContainer && recursive)
    {
    result = result.concat(findChildrenByType(obj as DisplayObjectContainer,
    typeClass, callback, recursive));
    }
    }
    return result;
    }

    trace(findChildrenByType(this, IEventDispatcher, myCallback))
    trace(findChildrenByType(this, MovieClip, myCallback))
    trace(findChildrenByType(this, TextField, myCallback))


  3. Default Re: Progaramatically declare Type

    On 2008-08-08 16:13:17 +0100, "Raymond Basque"
    <webforumsuser@macromedia.com> said:

    > childType:Object = null,

    that is the missing link
    thanks I didn't know there was an AS3 group LOL


+ Reply to Thread