Reputation: 365
I want the index to return to the first one as soon as its done processing all the items. This code doesn't seem to work. can somebody lend me a hand on how to fix this? why is this happening?
One hunch i have is since buttonArray.length
is not equaling to arrayPosition
it may be causing the issue. But I tried manually putting 3 and did (buttonArray.length-1)
. It still didn't work.
buttonArray.push (square,triangle,circle,hexagon);
function clickon(clickTarget:MouseEvent){
if (clickTarget.target == buttonArray[arrayPosition]){
trace ("correct");
trace (buttonArray.length);
trace (arrayPosition);
if (buttonArray[arrayPosition]== buttonArray.length)
{
arrayPosition = 0;
trace ("this is working");
}
else
{
arrayPosition++;
}
// inside if loop end
}
else
{
trace ("not correct");
}
}
Upvotes: 0
Views: 57
Reputation: 18747
You have a mistake in checking the position, you are checking the element against the array's length, but you should check the position itself.
if (arrayPosition == buttonArray.length)
Upvotes: 0
Reputation: 1302
Try changing:
if (buttonArray[arrayPosition]== buttonArray.length)
{
arrayPosition = 0;
trace ("this is working");
}
To:
if (arrayPosition == (buttonArray.length-1))
{
arrayPosition = 0;
trace ("this is working");
}
Upvotes: 1
Reputation: 3201
One way to make "arrayPosition" loop around from 0 to buttonArray.length can be to go:
arrayPosition++
arrayPosition = arrayPosition % buttonArray.length;
Example in a loop:
var buttonArray:Array = new Array(14); // array is 14 elements long
var arrayPosition:int = 0;
for(var counter:int = 0; counter < 100; counter++){
arrayPosition++
arrayPosition = arrayPosition % buttonArray.length;
trace(arrayPosition); // goes from 0 to 13, then back to 0 and repeats
}
Upvotes: 0