PDA

View Full Version : iterate through text boxes within movieclip


brianmcb
01-06-2006, 05:03 PM
As a Flash/Actionscript newbie, I have several incrementally named text boxes (fld1,fld2, fld3 etc) which I want to iterate through via ActionScript2 using a "for" statement to change their "visible" property.

Can someone give me a simple example of how to code it?

Also, from a resources point of view, which would be the most economical:

1. to programmatically toggle the visibility of 50 textboxes within a simple movie, or

2. to replace the existing movie with another where the visibility is the opposite value.

Thanks

flashead
01-06-2006, 05:14 PM
i dont think you need to worry about resources on this one, i could be wrong.

just use a for loop like so:

for ( var i=1; i<51; i++ )
{
scope["fld"+i]._visible = false; // or true
}

Where 'scope' is the target path to your text fields.
For example, if this code will sit at the same level as your text fields you would use:
this["fld"+i]._visible = false; // or true

or if your fields are in a movie clip called 'myMC', you would use:
myMC["fld"+i]._visible = false; // or true

brianmcb
01-06-2006, 06:02 PM
I tried what you suggested as follows:

Created a new movie, added a button (btn) to the stage, along with 2 text boxes (named nm1 and nm2), all on the same layer.

I added your code in a new layer called "actions" as follows:

comboListener = new Object();
comboListener.click = function(eventObj) {
for ( var i=1; i<3; i++ ){
this["nm"+i]._visible = false; // or true}
}
}
btn.addEventListener("click", comboListener);

saved the .fla and published it. Tested it and the boxes are still visible. Anything I'm not doing right?

flashead
01-06-2006, 06:09 PM
your scope is off, since you're using that code inside and object (comboListener), 'this' is local to that object.

set a variable to the scope of your fields and use that variable to target them:
var main = this;
//
comboListener = new Object();
comboListener.click = function(eventObj) {
for ( var i=1; i<3; i++ ) {
main["nm"+i]._visible = false; // or true
}
}
//
btn.addEventListener("click", comboListener);

brianmcb
01-06-2006, 07:21 PM
Copied and pasted your exact code into frame 1 of my "actions" layer:

var main = this;
//comboListener = new Object();
comboListener.click = function(eventObj) {
for ( var i=1; i<3; i++ ) {
main["nm"+i]._visible = false; // or true
}
}
//
btn.addEventListener("click", comboListener);

var main = this;
//comboListener = new Object();
comboListener.click = function(eventObj) {
for ( var i=1; i<3; i++ ) {
main["nm"+i]._visible = false; // or true
}
}
//
btn.addEventListener("click", comboListener);

Still no joy.

brianmcb
01-06-2006, 07:26 PM
I've got it to work with your code.

Sorry I noticed I'd commented out a line of your suggested code.

Thanks a lot for your effort Flashead.