Iterating through loop

while iterating through a loop, see code
the loop hangs at the last element of the array and does not move onto the temp1 screen...
Anyone???
Thank you
var AnsNum:Number = 0;
temp.move_next.onPress = function(){
	for(var AnsNum=1; i <= qArr.length ;i++)
 AnsNum++;
 try {
 if(AnsNum < qArr.length){

    trace("move to screen number " + AnsNum);
	//trace("Maximum: " + qArr.question[qArr.question.length - 1]);

  temp.q_txt.text= qArr[AnsNum].question;

		
          showscreen("temp");
		  
		 }else
		 {
	showscreen("temp1");
	}
 }catch(e:Error){
		trace("ERROR: goNext()");

Open in new window

Baxters801Asked:
Who is Participating?
 
dgofmanCommented:
Baxters801,
I don't know how you are implementing your moviclips and populated items into Array.
maybe some movie clips instances are missing or data in array.
The best what you can before run your for loop add a line

trace( qArr.toString())

and check how many elements in your array
0
 
dgofmanCommented:
You clean your code

temp.move_next.onPress = function(){
	for(var AnsNum = 0; AnsNum < qArr.length; AnsNum++){
		try {
			if(AnsNum < qArr.length - 1){ // -1 Last Answer show in temp1
				
				trace("move to screen number " + AnsNum);
				//trace("Maximum: " + qArr.question[qArr.question.length - 1]);
				temp.q_txt.text= qArr[AnsNum].question;
				showscreen("temp");
				
			}else
			{
				showscreen("temp1");
			}
		}catch(e:Error){
			trace("ERROR: goNext()"); 
		}
	}
};

Open in new window

0
 
Baxters801Author Commented:
This solution did not work. It took me through to the last element of the array with just one click of the next button and then did not advance to temp1..
Thank you for all of your help
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
dgofmanCommented:
Please can you attach your FLA or full code.

You may need to change:

temp.q_txt.text= qArr[AnsNum + 1].question;

But actually I cannot understand why you are looping inside "temp.move_next"

What is your requirements?

0
 
Baxters801Author Commented:
I cannot attach the full code.

I am looping through the array to populate a the dynamic textbox(temp.q_txt) n amount of times and then once the loop is completed move onto temp1 which does not display anything from this loop
0
 
Baxters801Author Commented:
the original code works fine until the last element and then it does not advance to temp1
0
 
dgofmanCommented:
In the original code you have a problem

var AnsNum:Number = 0;
for(var AnsNum=1; i <= qArr.length ;i++)
     AnsNum++;

You are declared AnsNum validating and incrementing "i"

is the same as

for(var AnsNum=1; AnsNum <= qArr.length ; AnsNum++)

But in your if condition you have to use -2

if(AnsNum < qArr.length - 2){

Example:
you have 5 elements, you want 4 elements making call using function showscreen("temp");
and last (5) showscreen("temp1");

In Arrays element index starts from 0

0-1, 1-2, 2-3, 3-4, 4-5

So, if you start your loop from 1 to <= 5 you have to use -2 not -1

if you will start index from 0 to < 5 your last index is array.length - 1



0
 
Baxters801Author Commented:
There are 8 elements in the array, 0 element is the first to populate from another function, in this loop i need to populate temp with elements 1 through 7 and then move on to temp1 which will not display any elements of the array but data from elsewhere...Temp1 works fine on its own, i just cant get temp to break once the 7 element has populated temp.
0
 
dgofmanCommented:
Please can you add trace function after you loop declaration and attach your trace
0
 
Baxters801Author Commented:
This is the trace which shows that is looping through to the end of the array
move to screen number 1
move to screen number 2
move to screen number 3
move to screen number 4
move to screen number 5
move to screen number 6
move to screen number 7
0
 
dgofmanCommented:
now modify your code and give me new trace

for(var AnsNum = 1; AnsNum <= qArr.length; AnsNum++){
		try {
			if(AnsNum < qArr.length - 2){ // -1 Last Answer show in temp1
				
				trace("move to screen number " + AnsNum);
				//trace("Maximum: " + qArr.question[qArr.question.length - 1]);
				temp.q_txt.text= qArr[AnsNum].question;
				showscreen("temp");
				
			}else
			{
                                                                                     trace("temp1 ="+ AnsNum );
				showscreen("temp1");
			}
		}catch(e:Error){
			trace("ERROR: goNext()"); 
		}
	}

Open in new window

0
 
Baxters801Author Commented:
Here is what i am getting


move to screen number 1
move to screen number 2
move to screen number 3
move to screen number 4
move to screen number 5
temp1 =6
temp1 =7
temp1 =8
move to screen number 1
move to screen number 2
move to screen number 3
move to screen number 4
move to screen number 5
temp1 =6
temp1 =7
temp1 =8
move to screen number 1
move to screen number 2
move to screen number 3
move to screen number 4
move to screen number 5
temp1 =6
temp1 =7
temp1 =8
0
 
dgofmanCommented:
What you expecting?
If you want to get 8 as temp1 remove "-2"
if you want to get 7 as temp1 change to "-1"
and loop condition

for(var AnsNum = 1; AnsNum < qArr.length; AnsNum++){
0
 
Baxters801Author Commented:
I do not want any of this array to be temp 1...I want to move to screen temp1 after the loop has finished. The dynamic textbox on temp 1 is pulling its value from another function....
0
 
dgofmanCommented:
I want to move to screen temp1 after the loop has finished. ???

Why do you need if condition in this case?

for(var AnsNum = 1; AnsNum <= qArr.length; AnsNum++){
	try {
		trace("move to screen number " + AnsNum);
		//trace("Maximum: " + qArr.question[qArr.question.length - 1]);
		temp.q_txt.text= qArr[AnsNum].question;
		//showscreen("temp");
	}catch(e:Error){
		trace("ERROR: goNext()"); 
	}
} 
showscreen("temp1");

Open in new window

0
 
Baxters801Author Commented:
Sorry, this isnt working either. I get an undefined after the first element is displayed and i click on next
0
 
Baxters801Author Commented:
thank you for all of your help...I hope i get this figured out soon...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.