常見的Flash programming惡習
filed in Flash on Oct.19, 2008 (1,280次檢視)
前言﹕
還記得Stanley初初接觸Flash的版本,應該仍是Flash 4的時候了,由玩timeline,只懂寫”gotoAndPlay”,”stop”等簡單的script做小遊戲,至到現在用as3.0做複雜的RIA,真是差不多十個年頭了。在Flash的發展上,編程的方法一直也支援在timeline上加script,到了as2.0的年代,更開始支援利用external的actionscript (.as file)。毫無疑問,這個編程模式對designer及programmer都照顧得到,相信這是能讓Flash普及的原因。
可是,這一個模式卻很多時做成Flash programming沒有了一個標準。更失望的是,很多雜誌及教人actionscript的書籍,一直都只是教人如何做到結果,卻很少教導程式結構及使用物件導向概念,以至很多Flash programmer做出來的Flash程式的質素十分惡劣。如果編程的人沒有心思,要接手他的爛攤子可真心痛呢…因此Stanley特別寫了這個題目,以我多年的Flash programming經驗告訴大家如何才可做到高質素的Flash application!
常見的Flash programming惡習
在這一個單元,我想先分享一些常見的惡習,要知錯才能改嘛!而且以下幾點,都是因為不懂使用物件導向概念才衍生的問題,值得留意哦。
1. 佈滿actionscript的timeline
在Flash中,我可以直接把actionscript放到timeline中,這的確十分方便。可是,對programmer來說,這是一個極大的惡習!首先,在管理上是十分不便的。很多時我都見到有人把actionscript加到不同movieclip、不同frame的不同位置,如果由另一個同事接手作少許的更新,他就要花很多時間去玩尋寶遊戲,找出及理解所有movieclip的actionscript了。而且,designer往往不清楚你寫的script幹麼,一不小心整個frame搬掉了,甚至刪除了,程式就會出錯,這都是經常發生的事呢。
另一個主要原因,就是programmer及designer不能分工同時工作,大大減低團隊效率!因為如果所有actionscript都在timeline上,即無論是UI及code,都是在同一檔案上,最終只會成為serial的工作模式。如果懂得把UI及code都能分開,那麼只要stage的movieclip結構定好了,designer及programmer都可以parallel方式工作,最後雙方把東西merge好就成。
2. 濫用root
初學actipnscript的人,經常不清楚如何取得movieclip,以至很多時都由root開始去取,例如:
root.myContainerMc.myNav.myBtn1
這方法的缺點,就是管理上有很大的麻煩。如果myNav改了名,或是myContainerMc被另一個新加的movieclip包著,那整個path也要改掉,因此錯誤的機會更高。
另一個問題主要是在as2.0多數發生。_root是指最頂層的main timeline,如B.swf用了_root,但是這個B.swf是會被A.swf load進來的,那麼在B.swf 用_root時,則會指向了A.swf的main timeline。要解決此問題就要用lockroot這個屬性解決,很麻煩哦…
3. 永無止境的parent.parent.parent…
parent 這一個屬性也是經常使用的,可是當movieclip的結構愈來愈複雜時,很多時都會出現parent.parent.parent…的script,非常不便,一當movieclip的結構有少許改變,script又要再更新,對於別人接手時也很難跟。
4. 一個class走天涯
比較advance的programmer,也懂得寫class把UI給code分開,可是他們不懂得活用物件導向的概念,把所有東西通通都寫在一個class中,使得整個class十分之長。同時因為同一個class要處理很多物件的script,使到function及variable的名字也十分複雜,如onPhotoViewerRollOver及onPhotoViewerThumbRollOver。但如果能為PhotoViewer及Thumb各寫一個class,而這兩個class也寫一個onRollOver的function,不是更整齊、更易明白?
5. 重覆又重覆的script
寫program時經常會有重覆的coding pattern,可是你有否想過把這些pattern歸納在一起? 例如在AS3.0中,向server作一個HTTP Request取數據的語法,都有三四句左右,可是,你有否想過你可以寫一個class,然後再define一個function,幫你完成Request及把傳回的XML轉化為XML Object? 如果你懂這樣,一定可以省回很多編程的時間!
好了,了解了以上這些通病,就得想辦法解決了,請留意我的下一篇文章,看看物件導向概念如何能夠幫助你吧!

December 19th, 2008 on 11:19 pm
寫完惡習, 唔係唔寫解決方法囉。
December 23rd, 2008 on 2:16 am
會架啦~ 等下先啦~
January 15th, 2009 on 12:14 am
真是一篇點中我學習as的問題所在阿…
能感覺的出真的很用心在寫文章,
感恩感恩~
January 15th, 2009 on 12:32 am
謝謝你的支持呢~
希望今個月可以準備好下一個章節~
January 15th, 2009 on 12:09 pm
我認為導至這些惡習,簡單講就是「神功不成,走火入魔」,在解決問題的前題下無所不用其極,拿些特效 source code 亂改一通,像碧眼狐狸弄錯心法練錯功,雖有一定功力,歪路卻越走越遠…
還請閣下大發慈悲多寫教學,救我等邪道歸入正途…
謝謝!
January 19th, 2009 on 4:31 pm
看來你很愛看武俠小說呢,希望新年假期時我可以寫幾篇Flash教學的文章~
May 8th, 2009 on 11:00 am
非常好的教學,真是受用,感謝您
May 27th, 2009 on 8:38 am
I am agree with your points, the book which in the book store, most will not tell you what is the function of syntax or script in the source code, although there many need a lot of page to explain that, I think that is necessary, because the learner may try to write their own program in the future, if he doesn’t know how the syntax to work in program, for example, if he is writing a OOP program, and he doesn’t know the difference between public and private, how can he to write derived class??
May 28th, 2009 on 12:13 pm
I would say not to get those Flash Books in local bookshop, like 大眾, 商務. Those books only talks about how to create visual effects in timeline level, which is for designer to read.
Flash now is not just a tool for your to create movies. You can use Flash to build useful Rich Internet Application!
Etsy.com is a web site which I love so much. Here is some amazing Flash from the site:
http://www.etsy.com/time_machine.php
http://www.etsy.com/connections.php
http://www.etsy.com/buy.php