一般情況下,源程序中所有的行都參加編譯。但有時希望對其中一部分內容只在滿足一定條件下才進行編譯,即對一部分內容指定編譯條件,這就是“條件編譯”(conditional compile)
條件編譯一般用來控制同一個項目的多個版本,如遊戲中,上不同平臺的版本需要實現不同的平臺接口.條件編譯可以在一個項目方便的控制版本的同時不增加編譯後的程序文件大小(這對 web 遊戲很重要,第一次加載的速度越快,則可能完成遊戲新手引導的人會越多.),同時還不損耗程序的性能.
具體的實現方式見下面的示例代碼:代碼中有很詳細的註釋,在FB中建個項目試一下就知道了.
-
package
-
{
-
import flash.display.Sprite;
-
import flash.text.TextField;
-
-
/**
-
* 條件編譯演示(在多版本控制中比較好用,比如下面指定的"中文版","日文版","英文版".在項目中也可以用來控制 web 版和 pad 版)
-
* <p>編譯後的代碼量不會增加,因它是編譯時進行處理的.<b>所謂的條件編譯是指:<font color="#ff0000">根據指定的條件:選擇性的編譯其中的某一部分</font></b></p>
-
* <p>需要在項目的編譯選項中附加如下編譯常量:</p>
-
* <listing version="3.0">
-
* -define=CONFIG::CHINESE,false
-
* -define=CONFIG::JAPANESE,false
-
* -define=CONFIG::ENGLISH,true
-
* </listing>
-
* 編譯選項的設置:
-
* <table>
-
* <tr>
-
* <td>flash builder</td><td>選擇要進行設置的項目 -> 右擊 -> 選擇"屬性" -> 選擇"ActionScript編譯器" -> "附加的編譯器參數"中指定</td>
-
* </tr>
-
* <tr>
-
* <td colspan="2">其它的參見:<a href="http://livedocs.adobe.com/flex/3/html/help.html?content=compilers_21.html">http://livedocs.adobe.com/flex/3/html/help.html?content=compilers_21.html</a></td>
-
* </tr>
-
* @author blank
-
*
-
*/
-
public class conditionCompile extends Sprite
-
{
-
/**
-
* 名字
-
* @return
-
*
-
*/
-
private function get pName():String{
-
CONFIG::CHINESE{
-
return "小明";
-
}
-
CONFIG::JAPANESE{
-
return "梅川庫子";
-
}
-
CONFIG::ENGLISH{
-
return "雞姆";
-
}
-
}
-
-
/**
-
* 國籍
-
* @return
-
*/
-
CONFIG::CHINESE{
-
private function get nationality():String{
-
return "中國"
-
}
-
}
-
CONFIG::JAPANESE{
-
private function get nationality():String{
-
return "日本"
-
}
-
}
-
CONFIG::ENGLISH{
-
private function get nationality():String{
-
return "英國"
-
}
-
}
-
-
public function conditionCompile()
-
{
-
var txf:TextField=new TextField();
-
txf.text="名字:" + pName + "\n" +
-
"國籍:" + nationality;
-
-
txf.width=txf.textWidth + 4;
-
txf.height=txf.textHeight + 4;
-
addChild(txf);
-
}
-
}
- }