Hi,
first of all, let me explain how the if statement works. So, everything can either be TRUE or FALSE, and an if statement is executed if the condition is TRUE, like, if you check if 5 is equals to 5, then that would be TRUE, meaning that the if statement would execute its codes, but if you checked if 3 was equals to 5, then it'd be FALSE, and the if statement would NOT work. So, if statement works only when the condition is TRUE, even though you may be using a boolean and checking if it's equals to false, because if it is, then that is also TRUE :P
Next thing to keep in mind is the difference between single equal sign and double equal sign:
= sets the left-side variable to the right-side value. Like,
click1 = true, then you're setting click1's value as TRUE
== checks if the left-side is equals to the right-side, if they match, and this is how you check if something is TRUE or FALSE
Your mistake was using
= in the if statement, rather than
==
But, why did the first if statement, even though you used single equal signs? Because, when you typed
if(click1=true), click1's value was first changed to TRUE, and then the if statement checked the value of click1, and since it was TRUE, the if statement was executed. In the second if statement, you first change click1's value to FALSE, and then the if statement checks its value, but since it's then FALSE, the if statement is not executed.
Corrected Code:
Code:
stop();
var click1:Boolean;
click1 = true;
if (click1==true) {
click1 = false;
b1.onPress = function() {
_root.gotoAndStop(10);
trace("1");
};
}
if (click1==false) {
b1.onPress = function() {
_root.gotoAndStop(20);
trace("2");
click1 = true;
};
}
That code will probably work, because in the second if statement -
if(click1==false) - if click1's value is indeed FALSE, then click1 equals to FALSE will return TRUE, because that condition is TRUE, and then the if statement will work. Same with the first if statement -
if(click1==true) - if click1's value is not TRUE (meaning it's FALSE), then click1 is equals to TRUE will return FALSE, because click1 is NOT then equals to TRUE, and thus the if statement will not work.
Hope this helps