2011-09-13

FLASH CS3 - Action Script part 7 CONDITIONAL

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?');
}  
If we notice above we will see untidy code.. but Above code able to tidy by using switch. The Format are like this
//=========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 admin
you are moderator
who are you? 
this is false right.. because it should show
you are admin
the 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 bird
is a plane
is superman
Yap.. 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
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

  1. me=='bird'
  2. other=='plane'
we can join both of conditional like this
     
         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: