A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: detecting text boxes

  1. #1
    will i ever get it?
    Join Date
    Feb 2004
    Posts
    707

    detecting text boxes

    i have an object that when entering the movie, needs to be "aware" of text boxes around it. Am i going about this the wrong way?

    Code:
    //detect input boxes, if any
    //start with the document class's objects
    			var main:Sprite=Sprite(stage.getChildAt(0));
    			var objs=main.numChildren;
    			for(var g=0;g<objs;g++)
    			{
    				var echob=main.getChildAt(g)
    				if(echob=='[object TextField]')
    				{
    					trace(echob);
    				}
    			}
    i feel like i am breaking away from the OOP style, but i can't think of another way to make this happen. Any thoughts out there?

    Bonus question...is there a cleaner way to write this part 'if(echob=='[object TextField]')'???

  2. #2
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    Try populating an array with the textfields and using hitTestObject to see if it hits them.

  3. #3
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    The OOP issue is that you're losing your objects and you have to look them up by their depths. I'm not sure what the scope of this is but if you are going to need an object later, its a good idea to store it in a var/array at a scope outside the function it's built in.

    Here's a quick rewrite:
    PHP Code:
    var myNewObj:Sprite = new Sprite();
    main.addChild(myNewObj);

    var 
    i:int main.numChildren;    //  loop variant
    var child:DisplayObject;        //  to be used in the loop

    while(0){
        
    i--;

        
    //  grab the display object
        
    child main.getChildAt(i);

        
    //  test if it's a TF
        
    if(child is TextField){
            
    //  if so trace it
            //  remember that it's cast to DO, not TF!!
            
    trace(child);
        }


  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    It is a bit distasteful to have to traverse the displaylist to get all textfields. By the way, the above code will only get the textfields that are direct children of the root, not anything embedded in another level.

    Depending on your requirements, you may be able to make a subclass of TextField which keeps track of all its own instances in a static array variable. You could then iterate over that array.

    Code:
    public class CollectedTextField extends TextField{
      public static allInstances:Array = new Array();
    
      public CollectedTextField(){
        super();
        allInstances.push(this);
      }
    }
    Now, if you can use CollectedTextFields instead of plain TextFields, you can do this later:

    Code:
    var ctf:CollectedTextField;
    for (var i:int=0; i < CollectedTextField.allInstances.length; i++){
      ctf = CollectedTextField.allInstances[i];
      //do something with ctf, such as hit test.
    }
    If you're more ambitious, you might want to create map or filter functions on the CollectedTextField class (static) and keep the instances array private.

  5. #5
    will i ever get it?
    Join Date
    Feb 2004
    Posts
    707
    so, if i go this way, which sounds cool, how to implement that subclass? I don't understand how the instances get pushed to that array. Does it imply that i have to push them from the document class? There are no dynamic instances....they are all there at runtime. So, if that is the case, how does this new subclasss "collect" all the instances to the array?

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I'm not 100% sure, as I don't have cs3 to play with, but if you can set the baseclass of your textfields to CollectedTextField, that should handle it since the constructor should be called automatically. I do not know if cs3 let's you do that easily.

  7. #7
    will i ever get it?
    Join Date
    Feb 2004
    Posts
    707
    i'll look into this....btw, i love your descriptor ".as-hole"- hahaha!! that's great

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Thanks. About time someone noticed. I've been pondering changing it to "A Taco", just to confuse people and make them wonder about MyFriendIsATaco (If you're reading mfiat, nothing implied there, I just find the idea funny).

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