flex as3 执行复杂的条件语句

来源:互联网 发布:java获取字符串编码 编辑:程序博客网 时间:2024/05/01 01:39
 问题
我要在多个条件中做出决定
解决办法
可以使用逻辑运算符AND (&&), OR (||), 和 NOT (!) 来创建符合条件语句。
讨论
 
ActionScript中的很多语句都能包含条件表达式。包括 if, while, 和 for 语句,如果测试两个条件都成立可以使用逻辑运算符 AND , &&, (更多细节请看第14章):
// 测试今天是否是3月17号
var current:Date = new Date(  );
if (current.getDate(  ) == 17 && current.getMonth(  ) == 3) {
  trace ("Happy Birthday, Bruce!");
}
加入些括号让结构更加清晰:
// Check if today is April 17th.
if ((current.getDate(  ) == 17) && (current.getMonth(  ) == 3)) {
  trace ("Happy Birthday, Bruce!");
}
这里使用了逻辑运算符OR , ||, 来测试是否其中有个条件成立:
// 测试是否是周末
if ((current.getDay(  ) == 0) || (current.getDay(  ) == 6) ) {
  trace ("Why are you working on a weekend?");
}
还可以使用 NOT, !, 来测试条件不是真的:
// 检测名字不是Bruce.
if (!(userName == "Bruce")) {
  trace ("This application knows only Bruce's birthday.");
}
上面的例子还可以这么写:
if (userName != "Bruce") {
  trace ("This application knows only Bruce's birthday.");
}

任何布尔值或能得出布尔结果的表达式都能作为测试表达式:
// 检测如果sprite 是可见的,则输出信息
if (_sprite.visible) {
  trace("The sprite is visible.");
}

NOT 运算符经常被用来检测是否是false:
// 检测如果 sprite 是不可见的,则输出信息:
if (!_sprite.visible) {
  trace("The sprite is invisible. Set it to visible before trying this action.");
}

NOT 经常和OR 一起用:
// 检测既不是Bruce 有不是 Joey.
if (!((userName == "Bruce") || (userName == "Joey"))) {
  trace ("Sorry, but only Bruce and Joey have access to this application.");
}
 
原创粉丝点击