-
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]')'???
-
Try populating an array with the textfields and using hitTestObject to see if it hits them.
-
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(i > 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);
}
}
-
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.
-
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?
-
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.
-
i'll look into this....btw, i love your descriptor ".as-hole"- hahaha!! that's great
-
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).