For Loop Problems?
I want to write a simple conditional statement
in which a button moves to a certain group of frames when clicked depending on its current frame. This is the code I'm using.
var group1:Array=["b1","b2","b3"];
var group2:Array=["b12","b22","b32"]
mainbutton.addEventListener(MouseEvent.CLICK, buttonflash)
function buttonFlash (e:MouseEvent)
{
for (var i:uint=0; i<group1.length; group1++)
{if
(e.target.currentLabel == group1[i])
{e.target.gotoAndStop(e.target.currentLabel + "2")}
else
{e.target.gotoAndStop(e.target.currentLabel.slice(0,2))}
}
}
However, when I include the for loop in the function, it causes the function to run the number
of times of the variable. In testing, I switched out the else statement with a trace string and the string showed up in the output window three times.
When I place the for loop outside of the function, the output compiler says the variable is undefined. Any ideas as to what I'm doing wrong?
for (var i:uint=0; i<group1.length; group1++)
start with i = 0 then while i is less than the length of the array group1 do something andthen add 1 to group1
this makes no sense
you need i++ so increases and the i<group1.length will eventually equate to false
changin group1++ to i++ may not solve the problem but it's definately a correct change
I am suprised group1++ even compiles
Sorry, that's a mistake in my first post. When I slimmed the code down for this post, I copy&pasted from the first for loop I use in the program, but forgot to change the variable names for the example I wanted to discuss.In the actual code all "i's" are replaced with "gr1's". It would be surprising if the code compiled with a mistake like that, but it's actually written correctly in the code I'm using.
So, yes, you're right that that would be a correct adjustment, but still that adjustment doesn't solve the problem. If the for loop is placed inside of the function it traces three times. If the for loop is placed outside of the function, the variable trace as "undefined". Any ideas as to why this is?
North America
Europe, Middle East and Africa
Asia Pacific