-
[RESOLVED] answer checker question [AS3][FLASH CS5]
hello,
I'm working on an app that gives you a random quiz and everything works exempt for checking if you have the right answers (and thats the important part ^^)
here is the script that i use to check if you have the correct answers and I'm sure that there is a better to do this but i don't know any and it also would help if i knew a quicker way:
[spoiler]
Code:
if (int(randomnumber) == 1){
if (stelling_2.selected == true){
score ++
}
}
if (int(randomnumber) == 2){
if (stelling_3.selected == true){
score ++
}
}
if (int(randomnumber) == 3){
if (stelling_1.selected == true){
score ++
}
}
if (int(randomnumber) == 4){
if (stelling_2.selected == true){
score ++
}
}
if (int(randomnumber) == 5){
if (stelling_1.selected == true){
score ++
}
}
if (int(randomnumber) == 6){
if (stelling_2.selected == true){
score ++
}
}
if (int(randomnumber) == 7){
if (stelling_2.selected == true){
score ++
}
}
if (int(randomnumber) == 8){
if (stelling_2.selected == true){
score ++
}
}
if (int(randomnumber) == 9){
if (stelling_3.selected == true){
score ++
}
}
if (int(randomnumber) == 10){
if (stelling_3.selected == true){
score ++
}
}
if (int(randomnumber) == 11){
if (stelling_1.selected == true){
score ++
}
}
if (int(randomnumber) == 12){
if (stelling_1.selected == true){
score ++
}
}
if (int(randomnumber) == 13){
if (stelling_3.selected == true){
score ++
}
}
if (int(randomnumber) == 14){
if (stelling_2.selected == true){
score ++
}
}
if (int(randomnumber) == 15){
if (stelling_1.selected == true){
score ++
}
}
if (int(randomnumber) == 16){
if (stelling_1.selected == true){
score ++
}
}
if (int(randomnumber) == 17){
if (stelling_3.selected == true){
score ++
}
}
if (int(randomnumber) == 18){
if (stelling_2.selected == true){
score ++
}
}
if (int(randomnumber) == 19){
if (stelling_1.selected == true){
score ++
}
}
if (int(randomnumber) == 20){
if (stelling_1.selected == true){
score ++
}
}
if (int(randomnumber) == 21){
if (stelling_2.selected == true){
score ++
}
}
if (int(randomnumber) == 22){
if (stelling_3.selected == true){
score +1
}
}
if (int(randomnumber) == 23){
if (stelling_3.selected == true){
score ++
}
}
if (int(randomnumber) == 24){
if (stelling_1.selected == true){
score ++
}
}
if (int(randomnumber) == 25){
if (stelling_3.selected == true){
score ++
}
}
if (int(randomnumber) == 26){
if (stelling_2.selected == true){
score ++
}
}
if (int(randomnumber) == 27){
if (stelling_1.selected == true){
score ++
}
}
if (int(randomnumber) == 28){
if (stelling_2.selected == true){
score ++
}
}
if (int(randomnumber) == 29){
if (stelling_2.selected == true){
score ++
}
}
if (int(randomnumber) == 30){
if (stelling_2.selected == true){
score ++
}
}
if (int(randomnumber) == 31){
if (stelling_1.selected == true){
score ++
}
}
if (int(randomnumber) == 32){
if (stelling_3.selected == true){
score ++
}
}
if (int(randomnumber) == 33){
if (stelling_2.selected == true){
score ++
}
}
if (int(randomnumber) == 34){
if (stelling_1.selected == true){
score ++
}
}
if (int(randomnumber) == 35){
if (stelling_3.selected == true){
score ++
}
}
if (int(randomnumber) == 36){
if (stelling_1.selected == true){
score ++
}
}
if (int(randomnumber) == 37){
if (stelling_3.selected == true){
score ++
}
}
if (int(randomnumber) == 38){
if (stelling_3.selected == true){
score ++
}
}
if (int(randomnumber) == 39){
if (stelling_2.selected == true){
score ++
}
}
if (int(randomnumber) == 40){
if (stelling_3.selected == true){
score ++
}
}
if (int(randomnumber) == 41){
if (stelling_1.selected == true){
score ++
}
}
if (int(randomnumber) == 42){
if (stelling_2.selected == true){
score ++
}
}
if (int(randomnumber) == 43){
if (stelling_3.selected == true){
score ++
}
}
if (int(randomnumber) == 44){
if (stelling_1.selected == true){
score ++
}
}
if (int(randomnumber) == 45){
if (stelling_1.selected == true){
score ++
}
}
if (int(randomnumber) == 46){
if (stelling_3.selected == true){
score ++
}
}
if (int(randomnumber) == 47){
if (stelling_1.selected == true){
score ++
}
}
if (int(randomnumber) == 48){
if (stelling_1.selected == true){
score ++
}
}
if (int(randomnumber) == 49){
if (stelling_2.selected == true){
score ++
}
}
if (int(randomnumber) == 50){
if (stelling_2.selected == true){
score ++
}
}
[/spoiler]
its just calling if's
thanks to everyone in advance!!!!
and if you need more info just ask ^^
-
Use a switch instead of ifs, do not repeat int(randomnumber). If you must use it more than once, save it in an appropriately typed variable.
I'd suggest associating the correct answers with the questions directly, so that you don't have to do a big if-then or switch, you just check whether the answer is correct for the question.
-
can you maybe go into more detail on how to do this?? im not realy good yet with AS3
-
i made the script into a switch but it still doesn't work:
PHP Code:
switch (randomnumber) { case 1 : if (stelling_2.selected == true) { score+1; } case 2 : if (stelling_2.selected == true) { score+1; } case 3 : if (stelling_2.selected == true) { score+1; } case 4 : if (stelling_2.selected == true) { score+1; } case 5 : if (stelling_2.selected == true) { score+1; } case 6 : if (stelling_2.selected == true) { score+1; } case 7 : if (stelling_2.selected == true) { score+1; } case 8 : if (stelling_2.selected == true) { score+1; } case 9 : if (stelling_2.selected == true) { score+1; } case 10 : if (stelling_2.selected == true) { score+1; } case 11 : if (stelling_2.selected == true) { score+1; } case 12 : if (stelling_2.selected == true) { score+1; } case 13 : if (stelling_2.selected == true) { score+1; } case 14 : if (stelling_2.selected == true) { score+1; } case 15 : if (stelling_2.selected == true) { score+1; } case 16 : if (stelling_2.selected == true) { score+1; } case 17 : if (stelling_2.selected == true) { score+1; } case 18 : if (stelling_2.selected == true) { score+1; } case 19 : if (stelling_2.selected == true) { score+1; } case 20 : if (stelling_2.selected == true) { score+1; } case 21 : if (stelling_2.selected == true) { score+1; } case 22 : if (stelling_2.selected == true) { score+1; } case 23 : if (stelling_2.selected == true) { score+1; } case 24 : if (stelling_2.selected == true) { score+1; } case 25 : if (stelling_2.selected == true) { score+1; } case 26 : if (stelling_2.selected == true) { score+1; } case 27 : if (stelling_2.selected == true) { score+1; } case 28 : if (stelling_2.selected == true) { score+1; } case 29 : if (stelling_2.selected == true) { score+1; } case 30 : if (stelling_2.selected == true) { score+1; } case 31 : if (stelling_2.selected == true) { score+1; } case 32 : if (stelling_2.selected == true) { score+1; } case 33 : if (stelling_2.selected == true) { score+1; } case 34 : if (stelling_2.selected == true) { score+1; } case 35 : if (stelling_2.selected == true) { score+1; } case 36 : if (stelling_2.selected == true) { score+1; } case 37 : if (stelling_2.selected == true) { score+1; } case 38 : if (stelling_2.selected == true) { score+1; } case 39 : if (stelling_2.selected == true) { score+1; } case 41 : if (stelling_2.selected == true) { score+1; } case 42 : if (stelling_2.selected == true) { score+1; } case 43 : if (stelling_2.selected == true) { score+1; } case 44 : if (stelling_2.selected == true) { score+1; } case 45 : if (stelling_2.selected == true) { score+1; } case 45 : if (stelling_2.selected == true) { score+1; } case 46 : if (stelling_2.selected == true) { score+1; } case 47 : if (stelling_2.selected == true) { score+1; } case 48 : if (stelling_2.selected == true) { score+1; } case 49 : if (stelling_2.selected == true) { score+1; } case 50 : if (stelling_2.selected == true) { score+1; } }
and this is the random number string:
PHP Code:
var randomnumber:String = (Math.floor(Math.random()*(1+high-low))+low).toString();
the labels of the stelling_# checkboxes are from an XML-file.
i want the script to check if i have as example checkbox_2 selected that it the var score get +1
if i am doing this wrong please tell me!
-
Why is your randomnumber a String? You are comparing it as an int. "1" is not equal to 1, so none of your switch cases will be triggered.
Also, you do not need the == true part. The condition of an if should be a boolean expression, which the selected property already is.
score+1 will evaluate, but you're not doing anything with the result, so it will just be thrown away. If you want the value in score to increment, you need to assign the result back to score, or use the ++ shorthand.
Any time you have the same case body in a row (in this case, all of them? then why do you need a switch at all?) then you can use fallthrough to collapse the switch for those cases.
Code:
switch (randomnumber){
case 1:
case 2:
if (stelling_2.selected){
score++;
}
break;
//... etc
}
When you do NOT want one case to continue executing the next case, you need to break. Your code has no breaks, so if randomnumber was 1, all 50 cases would execute. If it was 30, then the last 20 cases would.
But, really you should just store the correct answer for each question in a data structure that lets you look it up directly instead of having to use a switch. Although there's an even better way, let's start by assuming your questions are in order. Your answers should also be in order.
Code:
var answers:Array = [stelling_2, stelling_2, stelling_1, /* etc */];
Then just check the answer for the question.
Code:
if (answers[randomnumber].selected){
score++;
}
This assumes that stelling_2 and the rest are actually the same objects when you initialize the array as they are when you check the answer.
Last edited by 5TonsOfFlax; 10-25-2011 at 09:16 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|