|
New Member
Join Date: Feb 2003
Posts: 14
|
Using if..esle to test variables
Hi I wonder if anyone can help. I have created an activity which allows a user to drag and drop 2 commas into a sentence and get marked.
The program works by allowing the user to drag comma1 and place it in any of 7 targets. The targets are named incorrect1, incorrect2, incorrect3, incorrect4, incorrect5, correct1, correct2. Attempt1st variable is set to "True".
When the user drags and drops comma1 a hitTest is perfromed and the comma snaps into position.
if (_root.incorrect1.hitTest(comma1)) {
stopDrag();
Mouse.show();
_root.comma1._x = incorrect1XPosition;
_root.comma1._y = incorrect1YPosition;
counter++;
At this stage the user is free to move commas around from one position to another. When the user drags and drops counter is incremented and a "add 2nd comma" button appears.
if (counter == 1) {
setProperty ("next", _visible, true);
}
}
The "add 2nd comma" button takes the user onto the 2nd frame. On the 2nd frame the x and y position of comma1 are put into 2 variables and replaced by a static version of the comma called commaStatic1. The comma1 mc is unloaded. This prevents the user from dragging out comma1 by selecting "add 2nd comma" button they have committed. (the process is repeated for comma3 and comma4).
// Put x and y position of comma1 from "start" frame in variables
yposition = comma1._y;
xposition = comma1._x;
// Remove draggable comma mc
unloadMovie("comma1");
// Place static version of comma mc in place of draggable comma mc
_root.commaStatic1._x = xposition;
_root.commaStatic1._y = yposition;
Comma 2. When the user drags and drops counter is incremented and a "check" button appears (both answers have been selected for attempt1).
if (counter == 1) {
setProperty ("check", _visible, true);
}
}
The check button runs a function which takes the user to a frame named "check". If it is the first attempt, the first part of the if…else will run. Remember Attempt1st ="True" is set on the "start" frame.
The if .. Else statement checks where comma1Static and comma2 have been dropped. It sets the appropriate variable to "true" if commaStatic1 or comma2 have been dropped on incorrect1, incorrect2, incorrect3, incorrect4, incorrect5, correct1, correct2. It then removes commaStatic1 if it is in an incorrect position. An extract of the code is shown below.
// sets incorrect_flag1 = "True" and removes commaStatic1.
if (_root.commaStatic1._x == incorrect1XPosition)
{ _root.incorrect_flag1 = "True";
setProperty ("commaStatic1", _visible, false);
}
// sets incorrect_flag1 = "True" but at this stage comma2 has not been converted to commaStatic2 so it cannot be removed.
if (_root.comma2._x == incorrect1XPosition)
{ _root.incorrect_flag1 = "True";
}
Within the same if statement conditional logic checks whether both commas have been dragged and dropped in the correct location. If they have go to "right" frame.
If both answers are wrong set variable bothWrong = "True" otherwise I answer must be right and I answer must be wrong set oneWrong = "True".
// branch 1 if answer is right
if ((correct_flag1 == "True") && (correct_flag2 == "True"))
{
gotoAndStop("right");
}
// branch 3 if both answers are wrong
else if ((correct_flag1 == "False") && (correct_flag2 == "False"))
{
//set variable so that appropriate feedback can be given
bothWrong = "True";
gotoAndStop("1st wrong");
}
// branch 2 if one answer is right one answer is wrong
else {
oneWrong = "True";
gotoAndStop("1st wrong");
}
On the "1st wrong" frame all the incorrect_flag variables must be reset for 2nd attempt. If the user set either correct_flag1 or correct_flag2 these values must persist so that if they select another correct answer they will be marked correct and go to the "right" frame.
incorrect_flag1 = "False";
incorrect_flag2 = "False";
incorrect_flag3 = "False";
incorrect_flag4 = "False";
incorrect_flag5 = "False";
if comma2 is dragged and dropped in the wrong place it must be removed. For "check" frame Attempt1st is set to "false" and Attempt2nd to "true".
if (_root.comma2._x == incorrect1XPosition)
{ setProperty ("commaStatic2", _visible, false);
}
else if (_root.comma2._x == incorrect2XPosition)
{ setProperty ("commaStatic2", _visible, false);
}
else if (_root.comma2._x == incorrect3XPosition)
{ setProperty ("commaStatic2", _visible, false);
}
else if (_root.comma2._x == incorrect4XPosition)
{ setProperty ("commaStatic2", _visible, false);
}
else if (_root.comma2._x == incorrect5XPosition)
{ setProperty ("commaStatic2", _visible, false);
}
else if (_root.comma2._x == correct1XPosition)
{
}
else if (_root.comma2._x == correct2XPosition)
{
}
Comma3 in in the same position on "Ist wrong" as comma1 in frame1. The user can now drag comma3 and place it in any of 7 targets in the same way as before. The targets are again named incorrect1, incorrect2, incorrect3, incorrect4, incorrect5, correct1, correct2. From "check" frame if bothWrong = "True" then when user drags and drops "add 2nd comma" button appears (user can drag and drop 2 commas because both wrong commas were removed). If oneWrong =="True" then when user drags and drops "check" button appears (user can only drag and drop 1 comma because 1 comma is already correct).
if (oneWrong == "True") {
setProperty ("check", _visible, true);
}
else if (bothWrong == "True"){
setProperty ("next", _visible, true);
}
}
At this stage if oneWrong =="True" user place comma1 and selects "check" button. The check button runs a function which takes the user to a frame named "check
The 2nd part of the if..else statement runs if attempt2 =="True" (this was reset on 1st wrong).
The if .. Else statement checks where comma3 has been dropped. It sets a variable to "true" if comma3 have been dropped on incorrect1, incorrect2, incorrect3, incorrect4, incorrect5, correct1, correct2.
else if (attempt2nd == "True") {
// determine where comma was dropped and set flag for "check" frame (if one comma is selected)
if (_root.comma3._x == incorrect1XPosition)
{ _root.incorrect_flag1 = "True";
}
else if (_root.comma3._x == incorrect2XPosition)
{ _root.incorrect_flag2 = "True";
}
else if (_root.comma3._x == incorrect3XPosition)
{ _root.incorrect_flag3 = "True";
}
else if (_root.comma3._x == incorrect4XPosition)
{ _root.incorrect_flag4 = "True";
}
else if (_root.comma3._x == incorrect5XPosition)
{ _root.incorrect_flag5 = "True";
}
else if (_root.comma3._x == correct1XPosition)
{ _root.correct_flag1 = "True";
}
else if (_root.comma3._x == correct2XPosition)
{ _root.correct_flag2 = "True";
}
Then if both answers are right go to the "right" frame otherwise go to "2nd wrong" frame
if ((correct_flag1 == "True") && (correct_flag2 == "True"))
{
gotoAndStop("right");
}
// branch 3 if any answers are wrong
else {
gotoAndStop("2nd wrong");
}
Otherwise if bothWrong =="True" user places comma3 and selects "add 2nd comma" button.
On the next frame the x and y position of comma3 are put into 2 variables and replaced by a static version of the comma called commaStatic3. The comma3 mc is unloaded. This prevents the user from dragging out comma3. When the user drags and drops comma4 a hitTest is perfromed and the comma snaps into position.
The 2nd part of the if..else statement runs if attempt2 =="True" (this was set on 1st wrong).
The if .. Else statement checks where commaStatic3 and comma4 have been dropped. It sets a variable to "true" if commaStatic3 and comma4 have been dropped on incorrect1, incorrect2, incorrect3, incorrect4, incorrect5, correct1, correct2.
if (_root.commaStatic3._x == incorrect1XPosition)
{ _root.incorrect_flag1 = "True";
}
else if (_root.commaStatic3._x == incorrect2XPosition)
{ _root.incorrect_flag2 = "True";
}
else if (_root.commaStatic3._x == incorrect3XPosition)
{ _root.incorrect_flag3 = "True";
}
else if (_root.commaStatic3._x == incorrect4XPosition)
{ _root.incorrect_flag4 = "True";
}
else if (_root.commaStatic3._x == incorrect5XPosition)
{ _root.incorrect_flag5 = "True";
}
else if (_root.commaStatic3._x == correct1XPosition)
{ _root.correct_flag1 = "True";
}
else if (_root.commaStatic3._x == correct2XPosition)
{ _root.correct_flag2 = "True";
}
if (_root.comma4._x == incorrect1XPosition)
{ _root.incorrect_flag1 = "True";
}
else if (_root.comma4._x == incorrect2XPosition)
{ _root.incorrect_flag2 = "True";
}
else if (_root.comma4._x == incorrect3XPosition)
{ _root.incorrect_flag3 = "True";
}
else if (_root.comma4._x == incorrect4XPosition)
{ _root.incorrect_flag4 = "True";
}
else if (_root.comma4._x == incorrect5XPosition)
{ _root.incorrect_flag5 = "True";
}
else if (_root.comma4._x == correct1XPosition)
{ _root.correct_flag1 = "True";
}
else if (_root.comma4._x == correct2XPosition)
{ _root.correct_flag2 = "True";
}
// branch 1 if answer is right
if ((correct_flag1 == "True") && (correct_flag2 == "True"))
{
gotoAndStop("right");
}
// branch 3 if any answers are wrong
else {
gotoAndStop("2nd wrong");
}
}
Then if both answers are right go to the "right" frame otherwise go to "2nd wrong" frame
The first part of the porgram works fine. It allow the user to drag and drop 2 commas. IF both are wrong both get removed. If one is wrong the wrong one gets removed. If both are right the user is marked correctly.
But from "1st attempt" frame the program should allow the user to either drag and drop 1 or two comma in the correct location and go to the "right" frame. However it always seems to take them to the "2nd wrong" frame. I' m not sure why this is because on "1st wrong" frame all the incorrect variables are reset and any correct variables should persist. Meaning if the user selects either the one other right answer or both right answers the "check" frame should recognise
if ((correct_flag1 == "True") && (correct_flag2 == "True"))
{
gotoAndStop("right");
}
|