跳到主要內容

使用conditional compiling

在使用FlashCS4,CS5時,在Settings那有個Config constants欄位可以輸入
來紹介一下如何使用好了
簡單來說, 就是能定義一個常數, 給整個專案使用。
預設就有一個CONFIG::FLASH_AUTHORING,值為true


自定新增二個CONFIG::debug和CONFIG::release, 一個為true, 另一個為false
可以為Boolean,int,String等類別


怎麼取值呢? 很簡單,只要打上自定的Name名稱就可得到對映的值
進階用法, 可以透過該變數來決定要不要編譯該程式碼, 這功能非常的好用
Debug時, 可以將一些trace和測試用的程式碼編譯進去
但上線後, 以前可能要過變數來決定, 但還是會編譯進去,
現在使用這樣的方法來決定

寫一個測試用的類別, 加上判斷式。
package {
 import flash.display.Sprite;
 public class Demo extends Sprite {

  public function Demo() {
   trace("Demo.Demo");   
   CONFIG::debug {   
    trace("debug");
   }   
   bbb();
   ccc();     
  }
  public function aaa():void {
   CONFIG::debug {
    trace("test is true");
    return;
   }
   trace("test is false");   
  }
  
  CONFIG::debug
  public function bbb():void {
   trace("debug.bbb");   
  }
  
  CONFIG::release
  public function bbb():void {
   trace("release.bbb");   
  }
  
  public function ccc():void {
   trace("ccc");   
  }
 }
}
發怖後, 使用Sothink Flash Decompile來看一下

可以看到, 設成false的, 都不會被Compile進去, 酷也
那Flex或FlashDevelop怎麼使用呢?
在Compiler中加入對映的參數:-define=CONFIG::release,false
但每次都要去改設定值,覺得很麻煩, 想說可以把參數寫在xml, 這樣只要改xml的值
準備一份xml文件, 筆者取名為Config.xml
格式如下:

 
  
   CONFIG::debug
   false
  
  
   CONFIG::release
   true
  
 
然後在FlashDevelop的Compiler Options
Load Configuration File打上路徑, 就可以編譯了






參考文章:
Water and Bread: http://waterxbread.blogspot.com/2010/01/flexconditional-compiling.html
Flex Help:http://livedocs.adobe.com/flex/3/html/help.html?content=compilers_21.html
Inside RIA:http://www.insideria.com/2009/10/create-cleaner-actionscript-wi.html

留言

Vincent寫道…
HI M大: 請問一下 如果設定了 Load Configuration File 是否原本FD預設的CONFIG::debug ...都會被覆蓋掉?我試結果是這樣!!但我看編譯的LOG 是

mxmlc -load-config+=obj\PPP_newConfig.xml -load-config+=MYConfig.xml
都是寫' += ' ...感覺不是覆蓋.
Vincent寫道…
解決了
append="true" 加入這個就行了




CONFIG::DATE
2010/88/88
milkmidi寫道…
感謝提供的訊息