0

I am trying to make a video quiz and when you click it adds a value of 1 to an array. The array size goes up to [9] and I am trying to read from the array so that if there is a value of 1 in the array between [0] and [9] it will add 1 to the counter.

I have got it working for just the first value in the array so I know it works, but I am not sure about how to make it read from all of the array to check for values of 1.

Heres my code so far:

if (clickTimes[0] == 1)
{
counter2++;
}

5 Answers 5

2

Better yet, use a for each...in loop. It's really no different from a regular for loop, but it does provide some synactic sugar.

for each (var i:int in clickTimes)
{
    if (i == 1)
    {
        counter2++;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

For the beauty of it...

counter2 = clickTimes.join('').replace(/0/g, '').length;

It puts all your values in one string, remove the zeros and counts the characters left.

Comments

1
var clickTimes:Array = [1,1,1,0,0,0,1,1,0] // 5 1's and 4 0's
var counter2:int = 0

clickTimes.forEach(function(obj:*){
                       if (obj == 1){
                           counter2++;
                       }
                   })

trace(counter2) // traces 5

Comments

0

What about using a loop?

for (var i:int = 0; i < clickTimes.length; ++i)
{
    if (clickTimes[i] == 1)
        counter2++;
}

This would increment counter2, for each element that has a value of 1, in the clickTimes array.

10 Comments

Thanks for the quick response, this works for the just 1 click but doesn't seem to be working for anymore than 1. I am outputting counter2 in a dynamic text box if that helps.
@jjhilly So are you trying to increment counter2 if there is at least 1 click?
@david In frame 1 there is a video with errors in it, if you identify these by clicking in the right time it adds the value 1 to the array, there are 10 errors to identify. On the next frame it shows the results, so I am trying to read from the array and then add the score to a counter so that I can output it as a string in a dynamic text box.
@jjhilly Seems to me that there's an easier way to do this. Just make a score variable and increment it every time an error is identified, and set the text box to that value.
@David I have do as you have suggested, but it seems the whole problem may lie with my dynamic text box. code if (scores > 0) { resultText.text = String (+scores); } else { resultText.text = "0"; }
|
0

You do not need if. Just the following:

var clickTimes:Array = [1,1,1,0,0,0,1,1,0] // 5 1's and 4 0's
var counter2:int = 0;

for each (var i:int in clickTimes)
{
    counter2 += i;
}

trace(counter2); //5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.