Archive for the ‘Actionscript’ Category

好用的Flash Builder 快速鍵( hotkey )

Friday, November 13th, 2009

這也是從Adobe TV的MAX2009 session 聽來的,完整版請到adobe.tv看。

  • Ctrl+3 : 顯示快速command列表, 可以type-and-search執行所有的eclipse command
  • Ctrl + I : 自動縮排的妙用: 由別的文字檔貼過來的文字,全選後按下此快速鍵會自動排列。文字游標(I-Beam)移到任何地方按下此快速鍵,自動移到應該要去的地方,省得你按一堆tab和space
  • Ctrl+Shift+T:是open type, 這個大家都知道,但是奧妙在type-search的地方,可以打camel case string 作為搜尋;甚至可以用wildcard search. 舉例來說,想打開AdvancedDataGrid, 不用打Adv…去找,可打ADG就好( AdvancedDataGrid )。用wildcard search: 打 *skin 會列出所有 XXXskin相關的classes
  • Code assist : Alt+. (預設) 可以重複按,會自動filter分類,本來是顯示所有的屬性,每一次會顯示各類別: property, style, event

用Google Analytics 分析Flex應用程式

Tuesday, November 10th, 2009

Google 真的太威了…

安裝說明:
http://code.google.com/apis/analytics/docs/tracking/flashTrackingSetupFlex.html#getTheLibrary
取得library:
http://code.google.com/p/gaforflash/

注意:
GA tracking 的結果,不是及時的!
測試不是能夠直接看到結果的,必須等待幾個小時再到Google Analytics網站看報告。

你也在寫Flex嗎? 那你可能要特別注意…

Tuesday, November 10th, 2009

本篇文章來自MAX2009的session:
How Not to code FLEX

演講者主要說明何謂壞程式碼(bad code),但也說壞程式碼不一定不好,只要你確定能盡快回來改正他。那何謂壞程式碼?

  • Container 元件的誤用:Container元件諸如Box, Canvas, Tile等等,他們的原本目的都是: 排列裡面的子元件。因此只要你不是拿來排版或甚至沒有給予它任何的子元件,就違背原本用意了。
    例子:

    <mx:TextInput/>
    <mx:VBox backgroundImage="blah.png" borderStyle="solid"/> <!-- 你看, 沒有小孩的容器元件-->
  • ListBase 元件的誤用: List, TileList, DataGrid 都是用來針對列表物件的選取、排序、renderer回收而生,因此,當你想要製作的元件並不需要這些功能,就不該採用List來處理。
  • local model binding: 避免直接將值給UI元件,盡量用local model binding來處理。(按:此方法還可避免元件初始化延遲所帶來的問題)
    ex:

    <mx:script>

      //這樣是不好的
      function test():void
      {
         myLabel.text = "hello";
      }

    </mx:script>
    <mx:Label id="myLabel"/>
    <mx:Script>
    //用local model binding.
    [Bindable]
    private var helloText:String = "hello";
    </mx:Script>
    <mx:Label id="myLabel" text="{helloText}"/>
  • 別把local view event 交由非常上層的元件處理。(按:這裡必須搭配cairngorm microframework 來解釋 )
    元件與元件之間的訊息傳遞,如果能在local就解決,便盡量不要拋到上層的command-frontcontroller 去處理然後又繞回來”原本的元件”。以後維護會很辛苦。
  • 不適當的容器元件使用方法:
    如果你發現你的容器元件裡面只有一個子元件(child),那可能有問題。
  • Two dots and you are out.
    兩個參照你就出局囉!

    如果你的程式碼需要進入非常多的子元件,那就有問題了。如下:

    var hey:String = myBox.thisBox.thatBox.rightLabel.text;

    這是一個本質上的問題,應該要封裝這些子元件,若其他元件需要存取裡面的值,應該要透過Event(帶資料出來) 來幫你。

  • 自以為的聰明寫法並不好。
    這種情況會發生在複雜的binding寫法,像是:

    <mx:Button id="btn" enabled="{ isCheck &amp;&amp; ( firstBtn.enabled &amp; !secondBtn.enabled) &amp;&amp; (thirdBtn.enabled|| feelGood) }"/>

    應該要用function 去處理這些binding 邏輯:

    <mx:Script>
    private function getEnabled( a:Boolea , b:Boolea , c:Boolean , d:Boolean , e:Boolean ):Boolean
    {
    //邏輯應該在這裡描述啦:
    return a && (b&&!c) &&(d||e);
    }
    </mx:Script>
    <mx:Button id="btn" enabled="{ getEnabled(isCheck,firstBtn.enabled,secondBtn.enabled,thirdBtn.enabled,feeGood) }"/>

Adobe 官方推出拼字檢查元件( Squiggly ) for Flash/Flex

Thursday, September 24th, 2009

念法:
Squiggly [ˈskwigli] DJ [ˈskwɪglɪ] KK

下載:
http://labs.adobe.com/downloads/squiggly.html
Demo:
http://labs.adobe.com/technologies/squiggly/demo/

使用: Flex Unit Test Framework

Friday, September 11th, 2009

因應 XP 開發手法的趨勢,Adobe也推出FlexUnit: Unit test framework。但我個人覺得,Flex主要是關連到UI的,而UI的測試,光靠FlexUnit不夠的,用人測最直接。然而,通常你找不到那個人。

下載地點:Adobe open source project: Flex unit

何謂Unit Test? 簡單地來說:就每個Method當成一個unit(單位),測試之。在XP的開發過程中,是測試導向開發,什麼是”測試導向”? 就是:每次要寫新功能的時候,不是先寫功能的運作方法,而是先寫『測試該功能的程式』,換言之,我們要以”通過測試”為方向,去開發新功能的程式。因此,我們的開發流程往往是: 先寫空白的功能程式,譬如一個空的方法: getSomeResult(),然後先寫測試碼(test case),像是: testGetSomeResult() 然後寫上我們”預期的”測試結果,立刻跑一次test runner,當然,不會過關,立刻回頭撰寫getSomeResult()內容,讓它正確通過test case,那麼getSomeResult()即開發完成 。(見下方的步驟)

這樣的好處,除了確保開發出來的功能可以通過測試;往後的重構(refactor)或是需求變更,都可以再次把unit test 列出來跑一次,確保原來的功能沒有毀掉。
使用步驟:

  1. 建立TestRunner
  2. 寫TestCase/TestSuite並加入TestRunner
  3. 跑TestRunner
  4. 看結果,修改原始碼,回到(2)

例子:

//這是要被測試的class method

public class MyApp
{
      public function getSomeResult( input:Number ):Number
      {
           //這裡放入真正的邏輯...
           return input/10;
       }
}
//這是測試code( Test Case 不會用在真的主程式裡 ) : MyTestCase.as
private var classToTestRef:MyApp;
public function testGetSomeResult():void
{
     classToTestRef = new MyApp();
       /*assertEquals : 意思是兩個參數的值結果應該要一樣,
    如果不一樣test 就失敗(fail),你會在TestRunner看到
      底下這個例子會過!
       */

     assertEquals(  100 , classToTestRef.getSomeResult( 1000 ) );
      //這個不會過
     assertEquals(  2, classToTestRef.getSomeResult( 1000 ) );
      //還有許多assertXXX可以用,請見官方文件
}

特別要強調的是,針對Event處理的測試要怎麼寫? 因為Event發出的時間往往有時間差,並不即時。FlexUnit體貼地加上了一個addAsync() 的方法,幫助我們測試event listeners。寫法如下:

addAsync( eventListener:Function/*放置assertion的listener*/ , timeout:int /*過了多少時間(ms)就算失敗?*/);
public function testVeryComplexCalulation():void
{
    //用addAsync() 把test case 延遲檢查:
    instanceToTest.addEventListener( Event.COMPLETE ,
    addAsync( onComplete , 1000 /*設定:1秒沒發生就算測試失敗*/) );  
    instanceToTest.run();
}

//在 event listener 裡面才寫 assertions:
private function onComplete( event:Event ):void
{
   assertNotNull( instanceToTest.result );
}

Event, EventDispatcher, Listener

Thursday, August 13th, 2009

這三者的關係,可以用一個情境來解釋:
總機先生,電話機,通話內容。

總機先生是 Listener
電話是 EventDispatcher
通話內容是 Event

通話內容必須透過電話傳達出去,出去之後就由總機先生立刻接聽;此外,一通電話內容可以讓無限個總機接聽(想像把電話機設定成擴音模式)。這個模式的作用在於,讓總機先生在『適當的時間』,做『適當的事』,比如說,一接到找老闆的電話,就要先問:『您是哪位?』此時『一接到電話』就是Event 透過EventDispatcher 發出來了,又正好總機監聽著電話狀態,所以總機立刻根據電話內容(event),做了特定的事情(問:您是哪位?)。再舉個例子,像是接到愛慕的A氏,就回答:『哈囉你好嗎?』 這兩種回達都是一個Listener ,根據不同的事件,定義不同的listener。

Listener 在 AS3.0 的世界,規定要寫成:

function functionName( event:Event ):void

要監聽某個電話就要寫成:
myPhone.addEventListener( eventType , myListener );
其中eventType是個字串,代表事件的類別(老闆打的還是同事打的),myListener 就是呼應的動作。要特別強調的是,在AS的世界裡,當一個Event被dispatch出來之後,對應的listener幾乎『沒有時差』就發動,因此我們常遇到UIComponent繪製畫面不如預期,這時可在listener之中利用 callLater 來針對UIComponent操作,利用callLater的延遲是比較正統的作法。

強制結束DragManager

Thursday, July 23rd, 2009
DragManagerImpl#endDrag();

見:DragManagerImpl.as
由於原本的DragManager實作了IDragManager,裡面並沒有公開endDrag()此signature,因此你必須轉型之後才能呼叫這個方法。

清除預設的DragEvent外觀效果

Friday, May 22nd, 2009

針對Flex 預設的Drag-Drop,List元件會貼心地幫你畫出indicator(預設是一條線),然而,一旦試著用 DragEvent#preventDefault()時,indicator 會殘留在畫面上,此時,可以利用

ListBase#hideDropFeedback(event:DragEvent)

即可清除掉indicator, highlight 等 DisplayObject.

==
To clean up Flex default DragDrop visualization, try ListBase#hideDropFeedback(event), Especially when implementing your own drag-drop event handlers. This method remove all highlight and indicator display objects.

英文單字單複數互轉(Actionscript Inflection Utility)

Tuesday, May 19th, 2009

模仿Rails的Inflection方法,用actionscript 改寫如下:

Inflection#singularize : 將複數轉單數

如:
Inflection.singularize( “cars” ); //cars => car

Inflection#pluralize : 將單數轉複數

如:
Inflection.pluralize( “apple” ); // apple => apples

原始碼如下:(source)

package net.shiue.util
{
public class Inflection
    {
        public function Inflection()
        {
        }
       
        public static function pluralize( word:String ):String
        {
           
            if( isUncountable( word ) )
                return word;
           
            for each( var r:Replacer in plurals )
            {
                if( word.search( r.pattern ) >= 0 )
                    return word.replace( r.pattern , r.repalcePattern );
            }
           
            return word;
        }
       
        public static function singularize( word:String ):String
        {
           
            if( isUncountable( word ) )
                return word;
           
           
            for each( var r:Replacer in singulars )
            {
                if( word.search( r.pattern ) >= 0 )
                    return word.replace( r.pattern , r.repalcePattern );
            }
           
            return word;
        }
       
        private static var _plurals:Array;
       
        private static function get plurals():Array
        {
           
            if( _plurals == null )
            {
                _plurals = [];
               
                _plurals.push( new Replacer( /(quiz)$/i , "$1zes" ) );
                _plurals.push( new Replacer( /^(ox)$/i , "$1en" ) );
                _plurals.push( new Replacer( /([m|l])ouse$/i , "$1ice" ) );
                _plurals.push( new Replacer( /(matr|vert|ind)(?:ix|ex)$/i , "$1ices" ) );
                _plurals.push( new Replacer( /(x|ch|ss|sh)$/i , "$1es" ) );
                _plurals.push( new Replacer( /([^aeiouy]|qu)y$/i , "$1ies" ) );
                _plurals.push( new Replacer( /(hive)$/i , "$1s" ) );
                _plurals.push( new Replacer( /(?:([^f])fe|([lr])f)$/i , "$1$2ves" ) );
                _plurals.push( new Replacer( /sis$/i , "ses" ) );
                _plurals.push( new Replacer( /([ti])um$/i , "$1a" ) );
                _plurals.push( new Replacer( /(buffal|tomat)o$/i , "$1oes" ) );
                _plurals.push( new Replacer( /(bu)s$/i , "$1ses" ) );
                _plurals.push( new Replacer( /(alias|status)$/i , "$1es" ) );
                _plurals.push( new Replacer( /(octop|vir)us$/i , "$1i" ) );
                _plurals.push( new Replacer( /(ax|test)is$/i , "$1es" ) );
                _plurals.push( new Replacer( /s$/i , "s" ) );
                _plurals.push( new Replacer( /$/i , "s" ) );
               
            }
           
            return _plurals;
        }
       
        private static var _singulars:Array;
       
        private static function get singulars():Array
        {
            if( _singulars == null )
            {
                _singulars  = [];
               
                _singulars.push( new Replacer(/(database)s$/i , "$1") );
                _singulars.push( new Replacer(/(quiz)zes$/i , "$1") );
                _singulars.push( new Replacer(/(matr)ices$/i , "$1ix") );
                _singulars.push( new Replacer(/(vert|ind)ices$/i , "$1ex") );
                _singulars.push( new Replacer(/^(ox)en/i , "$1") );
                _singulars.push( new Replacer(/(alias|status)es$/i, "$1") );
                _singulars.push( new Replacer(/(octop|vir)i$/i, "$1us") );
                _singulars.push( new Replacer(/(cris|ax|test)es$/i, "$1is") );
                _singulars.push( new Replacer(/(shoe)s$/i, "$1") );
                _singulars.push( new Replacer(/(o)es$/i, "$1") );
                _singulars.push( new Replacer(/(bus)es$/i, "$1") );
                _singulars.push( new Replacer(/([m|l])ice$/i, "$1ouse") );
                _singulars.push( new Replacer(/(x|ch|ss|sh)es$/i, "$1") );
                _singulars.push( new Replacer(/(m)ovies$/i, "$1ovie") );
                _singulars.push( new Replacer(/(s)eries$/i, "$1eries") );
                _singulars.push( new Replacer(/([^aeiouy]|qu)ies$/i, "$1y") );
                _singulars.push( new Replacer(/([lr])ves$/i, "$1f") );
                _singulars.push( new Replacer(/(tive)s$/i, "$1") );
                _singulars.push( new Replacer(/(hive)s$/i, "$1") );
                _singulars.push( new Replacer(/([^f])ves$/i, "$1fe") );
                _singulars.push( new Replacer(/(^analy)ses$/i, "$1sis") );
                _singulars.push( new Replacer(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, "$1$2sis") );
                _singulars.push( new Replacer(/([ti])a$/i, "$1um") );
                _singulars.push( new Replacer(/(n)ews$/i, "$1ews") );
                _singulars.push( new Replacer(/s$/i, "") );
               

            }
           
            return _singulars;
           
        }
       
        private static var _irregular:Array;
       
        private static function get irregular():Array
        {
            if( _irregular == null )
            {
                _irregular = [];
               
                _irregular.push( new Replacer(/person/i , "people") );
                _irregular.push( new Replacer(/man/i , "men") );
                _irregular.push( new Replacer(/woman/i , "women") );
                _irregular.push( new Replacer(/child/i , "children") );
                _irregular.push( new Replacer(/sex/i , "sexes") );
                _irregular.push( new Replacer(/move/i , "moves") );
                _irregular.push( new Replacer(/cow/i , "kine") );
            }
           
            return _irregular;
        }
           
        private static var _uncountable:Array;
       
        private static function get uncountable():Array
        {
            if( _uncountable == null )
            {
                _uncountable = [];
               
                _uncountable.push("equipment");
                _uncountable.push("information");
                _uncountable.push("rice");
                _uncountable.push("money");
                _uncountable.push("species");
                _uncountable.push("series");
                _uncountable.push("fish");
                _uncountable.push("sheep");
               
            }
           
            return _uncountable;
        }
       
        public static function isUncountable( word:String ):Boolean
        {
            return uncountable.indexOf( word )>=0;
        }

    }
   
   
}
   

    class Replacer
    {
        public function Replacer( pattern:RegExp , replacePattern:String )
        {
            this.pattern = pattern;
            this.repalcePattern = replacePattern;
        }
       
        public var pattern:RegExp;
        public var repalcePattern:String;
    }

製作蘋果電腦的標題列外皮(Mac title bar skin)

Tuesday, May 12th, 2009

Mac的比較漂亮啦 !

剛剛研究出快速畫出Mac標題列的樣式,這麼做:
建立自己的ProgramaticSkin, 覆寫 updateDisplayList( w , h );

override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
super.updateDisplayList( unscaledWidth , unscaledHeight );
var g:Graphics = graphics;
g.clear();

var m:Matrix = verticalGradientMatrix( 0 , 0 , unscaledWidth , unscaledHeight );
drawRoundRect( 0 , 0 , unscaledWidth , unscaledHeight , { tl:6,tr:6,bl:0,br:0 } ,
          [ 0xC5C5C5 , 0x969696 ] , [1,1] ,m , GradientType.LINEAR , [0,255] );

}