Related to previous Flash Tutorial.. We learn basic from conditional. As we know, the main problem I see in this conditional is miss-variable type. As you already learn from previous. Now let's learn the conditional, to make conditional we can use IF and switch
Let's make simple conditonal here.. You see a car then you asked what do you see. If you said car, the answer is correct.
if(answer == 'car'){
trace('Correct');
}
let's add the script.. If you said wrong, Answer is false.. and We have like this
if(answer == 'car'){ trace('Correct');}else{trace('False');}
For more complex.. We will have
if(login=='admin')
{
trace('you are admin');
}else if(login=='moderator'){
trace('you are moderator');
}else{
trace('who are you?');
}
//=========SWITCH=========
switch (testExpression) {
case caseExpression:
// case body
case caseExpression:
// case body
default:
// case body
}
Then after I fixed above script with switch.. we have this
switch (login) {
case "admin":
trace('you are admin');
case "moderator":
trace('you are moderator');
default:
trace('who are you?');
}
But if you type like this.. this have an error .. If login == admin I will have
you are adminthis is false right.. because it should show
you are moderator
who are you?
you are adminthe problem is on the break. If you don't give break before next expresion, the code will continue to the next expresion. Let's fixed this out and we have like this
switch (login) {
case "admin":
trace('you are admin'); break;
case "moderator":
trace('you are moderator');
break;
default:
trace('who are you?');
}
for the last expresion, we don't need break because is already the last. Default expresion mean, if there login is not same as other expresion, default will be run.
COMPLEX CONDITIONAL
Do you remember this person?
Nah.. I know he's not me.. If we see in old movies.. someone will said this
is a birdYap.. I will use this superman as an example to my script. If I see a bird (i'm not sure), then someone say is a plane (I think he not sure either).. I think.. it is Superman.. or perhaps both of us need a good pair of glasses
is a plane
is superman
actualy my glasses is gone?!?okay.. this is the code
if(me=='bird' && other=='plane')
{
trace('Is Superman!!');
}else{
trace('We need glasses');
}
As you see here.. I type if I said Bird, other said plane.. It is Gotta be Superman!! If you see here.. there are 2 conditional
- me=='bird'
- other=='plane'
if(me=='bird' && other=='plane')
&& is an operator for AND! if both conditional are true the result will be true also!TRUE | FALSE | |
TRUE | TRUE | FALSE |
FALSE | FALSE | FALSE |
Like this table.. Another operator is OR ( || ). Unlike AND, if one of both are TRUE.. the result will be TRUE
TRUE | FALSE | |
TRUE | TRUE | TRUE |
FALSE | TRUE | FALSE |
hmmm, the example script :
if(me=='bird' || other=='bird')
{
trace('Is a Bird!!');
}
If I see Bird or next to me see a bird.. It's a Bird. Not only simple conditional, you can use NOT OPERATOR (!).
Next we will try make repeating operation using For,while etc
Tidak ada komentar:
Posting Komentar