Copy link to clipboard
Copied
When I compare cdlCorrect[index] to 0, it shows true, but when I trace it, it shows 2.
The if statement seems to be comparing the index itself to 0. How do I make it compare the value. In fact, as I keep calling this function (clicking the button), it always compares to 0, but the trace will come back as 2, then 1, then 2, then 0, then 0. I would think I am getting the index, but then it should compare to 0, then 1, then 2 etc. I do not understand why it always compares with zero.
function chkAnswer(mevt:MouseEvent):void {
cdlDone[index] = 1;
var rectSize:uint = 300;
var correctRect:Shape = new Shape();
correctRect.graphics.beginFill(0x00FF00, 0.5);
if (cdlCorrect[index] == 0) {
correctRect.graphics.drawRect(45, 40, rectSize, 30); //coordinates: 45:x 120:y rectSize:width 50:height
trace("This is 0");
}
if (cdlCorrect[index] == 1) {
correctRect.graphics.drawRect(45, 80, rectSize, 30); //coordinates: 45:x 120:y rectSize:width 50:height
trace("This is 1");
}
if (cdlCorrect[index] == 2) {
correctRect.graphics.drawRect(45, 80, rectSize, 30); //coordinates: 45:x 120:y rectSize:width 50:height
trace("This is 2");
}
trace(cdlCorrect[index]); // 2
trace(cdlCorrect); // 2,1,2,0,0
}
If you are reading the values in from an xml file then you either need to convert them into numbers or you need to test them as strings.
try changing to
if(cdlCorrect[index] == "0"){
and the same for the others as well
Copy link to clipboard
Copied
What is defining the value of 'index'? Try tracing that value so you know what value of the array is being tested.
Copy link to clipboard
Copied
index is an int value. it represents the index into the array. If you look at the code, you cans see that the value supposedly in cdlCorrect[index] = 2. The values in cdlCorrect, which represent the correct answer position are read into the array, from a file, via an XML class. Trace cdlCorrect and you see the answer to the 5 questions read into radio buttons. i.e.. 2, then 1, then 2, then 0 and finally 0, meaning that the answer is the 3d radio button, followed by the 2nd radio button, followed by the 3d radio button, followed by the 1st radios button and then the last question's answer is in the 1st radio button.
My question is why does the if statement always show that cdlCorrect[index] = 0, even when the trace(cdlCorrect[index]) shows it as 2? I am missing something here. Is that trace shows 2, shouldn't the if statement be true when compared to 2 and NOT 0?
I understand that the 2 may possible be an ASCII Hex 32, instead of a binary 0010, but it still should not compare to zero.
Can you explain how the if statement can indicate the value is 0, and the trace statement can indicate the value is 2?
Copy link to clipboard
Copied
If you are reading the values in from an xml file then you either need to convert them into numbers or you need to test them as strings.
try changing to
if(cdlCorrect[index] == "0"){
and the same for the others as well
Copy link to clipboard
Copied
Yes, that was the main part of the answer. The other should have never happened. The person I was trying to help, actually changed the variable. Thankfully I asked them to read me what they had in the first if statement. Once the correct variable (cdlAnswer[index]) was put back and tested (did not work), I had them add the quotes you suggested and everything started working for them. It's a bummer trying to work with someone over the phone. Thanks
Copy link to clipboard
Copied
You're welcome
Find more inspiration, events, and resources on the new Adobe Community
Explore Now