PDA

View Full Version : For Loop and ComboBoxes help


Continuity-B
06-28-2007, 02:26 PM
I have var numOfCombos:Number which is the number of combobox instances on stage, say 4, (I am making myself a template) which is used to define the range of a the for-loop below....


for (var i:Number = 1; i<=numOfCombos; i++) {
_root["cbBox_"+i]._x = 100, ;

}

I've traced i and published it and it works perfectly. But the thing is it was intended only a precursor to this code...

for (var i:Number = 1; i<=numOfCombos; i++) {
_root["cbBox_"+i].selectedIndex = 2;

}

...which does nothing. I am trying to make my comboboxes jump to a correct answer, in the end this will run when a button is pushed and the selectedIndex number will be a variable relating to the correct index for each combobox. But I want to know why this is not working?!

Thanks in advance
Scott

Continuity-B
06-28-2007, 02:56 PM
If you absolutely must move my thread, fine, but I wouldn't mind being told where so that I can find it again without having to hunt - it's fairly irritating.

Anyway, I hope someone can still help, thanks.

Continuity-B
06-28-2007, 03:13 PM
Right I see now the problem is that the indices are undefined... I thought they would be set when i used addItemAt to fill the comboBoxes?

CyanBlue
06-28-2007, 03:13 PM
Now I suddenly feel really guilty... But you are responsible for posting your question to the right forum and you cannot imagine how many threads I have to move because of the various reasons... and I'd rather use that time to help somebody instead of moderating the forum... :(

At any rate, you can use this link to review all your posts...
http://www.actionscript.org/forums/subscription.php3?do=viewsubscription&folderid=all

As for your question, that syntax is totally valid... Maybe there is something else going on...
I've attached a sample file to show you that it works fine... Maybe you might want to double check it with what you have, and post more details if they look the same...

Continuity-B
06-28-2007, 03:36 PM
Thanks for your reply - I can see all the forums and I thought it more of an AS problem but anyway nevermind.

The difference I can see is that you have populated the cbs with the inspector and I have used...

cbBox_1.addItemAt(0,"one");
cbBox_1.addItemAt(1,"two");
cbBox_1.addItemAt(2,"three");
cbBox_1.addItemAt(3,"four");

... as I find the inspector clunky for repeated use when creating lots of these interactivities and I'm trying to write wholly AS solution.

I tested a 'selectedIndex =' OUTSIDE of the for-loop and it works fine, inside it does nothing and when I try to trace an index number inside the loop it is undefined.

Here is my full AS so far...

// import the comboBox class
// allows us to declare the instances and get approcode hints
import mx.controls.ComboBox;

// declare symbol instances
// gives appropriate code hints
var cbBox_1:ComboBox;
var cbBox_2:ComboBox;
var cbBox_3:ComboBox;
var cbBox_4:ComboBox;

// declare variables
// numOfCombos == the total number of comboBox instances on stage
// will be used in a 'for loop' to dynamically repeat the same code for each comboBox when a button is pressed
var numOfCombos = 4;
// correctN == the index of the correct answer in each comboBox
var correct_1:Number = 0;
var correct_2:Number = 1;
var correct_3:Number = 2;
var correct_4:Number = 3;

//functions

for (var i:Number = 1; i<=numOfCombos; i++) {
// trace here to check the values for i are correct
//trace(i);
// this doesn't work
_root["cbBox_"+i].selectedIndex = 1;
}


// populate fields in each comboBox with 'addItemAt(position:Number, label:String)'
// you can use addItem(label:String) to add labels consecutively but with addItemAt we can see the position number for reference
cbBox_1.addItemAt(0,"one");
cbBox_1.addItemAt(1,"two");
cbBox_1.addItemAt(2,"three");
cbBox_1.addItemAt(3,"four");
//
cbBox_2.addItemAt(0,"five");
cbBox_2.addItemAt(1,"six");
cbBox_2.addItemAt(2,"seven");
cbBox_2.addItemAt(3,"eight");
//
cbBox_3.addItemAt(0,"nine");
cbBox_3.addItemAt(1,"ten");
cbBox_3.addItemAt(2,"eleven");
cbBox_3.addItemAt(3,"twelve");
//
cbBox_4.addItemAt(0,"thirteen");
cbBox_4.addItemAt(1,"fourteen");
cbBox_4.addItemAt(2,"fifteen");
cbBox_4.addItemAt(3,"sixteen");

// run now
// this works
//cbBox_1.selectedIndex = 1;

CyanBlue
06-28-2007, 03:41 PM
What happens if you move that for loop block down below where it says // run now??? Does that make any difference???

Continuity-B
06-28-2007, 03:48 PM
Ha ha yeah that's it !

So obvious in hindsight, always the way though...

Thanks a lot, looks solved now !

Scott

CyanBlue
06-28-2007, 03:52 PM
Yup... The trouble was that you were trying to set the selectedIndex of the component that had nothing in it, so it did not work...