同上一篇開發air小技巧一起, 我想寫一篇開發website的小技巧
其實對于as3來說, website開發既簡單, 又復雜. 你可以用最簡單的方式來處理, 也可以像我一樣寫個簡單的小架構, 當然調試和發布也是要分開的
簡單方式就是loadmovie
很多ui設計師不會復雜的程序, 于是就產生了簡單的laodmovie, 你不用去想如何去卸載, 什么時候加載完畢
設計師想卸載完全可以在這個容器基礎上加載個空影片
簡單的架構:
我習慣于給網站或者應用寫個簡單的架構
它需要如下特點:
1.耦合性不高
2.層次分明
3.開發快速
對于第一點有幾方面要說, 先從event事件開始, 首先, 你不可能保證你互相傳遞信息的對象都是在彼此冒泡級序列上, 也不可能保證直接去調對象去完成事件偵聽或者直接對對象進行處理, 因為這樣耦合性很低, 網站或程序開發動向改變會直接影響到大部分代碼的修改以及各種問題
這里我根據xcode和cocos2d結構寫了個NotificationCenter通知類, 這個類很簡單, 就是一個單例, 并控制事件的集合任何運行周期, nc類都是只存在一個對象, 所以十分安全, 即便局部開發也不影響, 而且即便是更改結構, 也不影響nc傳遞, 頂多沒有對象發事件或者沒有對象接收事件而已
在同一個ide內的不同元素, 通過nc事件將他們分割開, 每個按鈕或者面板都是獨立的內容, 且繼承movieclip類, 這樣也十分靈活, 美工刪除或者添加都不會造成問題, 而這是之前as2時代所具有的特色
一般我會用到fd開發網站或者應用, 主要是因為編譯速度快, 輕量級, 提示方便
而整個網站如果全部用fd開發, 那太龐大了, 我一般這樣的結構
loader ?---? fd開發
其他swf ?---? ide開發
fd很方便的可以直接調用ide編譯(f6), 自編譯(f5), 這樣就能分別處理不同的fla文檔
fd中將會包含只針對fla的代碼和只針對fd的代碼, 因為ide直接輸出swf, 所以需要部分fd代碼參與, 其中nc就是重中之重
而開發ide代碼只需要在綁定對象的類中直接聲明public var對象元素就可以方便在fd中編譯, 而且不會報錯
同時強調如果是通過ide輸出結構(位置界面等信息)的swc, 則可能只能得到簡單的對象結構, 而不含開發代碼
層次分明
用loader對分層加載進行管理
一般層次劃分是這樣的:
警告提示層 --- 1
彈出框 ?2
菜單/logo層 3
二級內容 4
一級內容 5
背景層 6
其中 4,5,6可以使一個層
結構是這樣, 因為其他層都很簡單, k數也小, 基本不做loading顯示
一般只對主層進行加載loading展示
所謂開發簡單,速度快,不僅是前期, 后期也是一樣, 很多情況下你需要和美工一起來解決問題, 而客戶的所有問題又不針對技術, 這樣會導致效率低下, 美工改一次你就要改一次, 而如果不需要動loading來講的話, 美工可以自行生成fla, 不需要技術再進行修改, 這樣也降低了耦合性
網站改版或者修改十分頻繁, 所以以這樣一個思路來制作, 是十分有必要的
下面貼出來nc的類, 希望大家可以借鑒, 其實還有很多敲門, 比如AutoCount這樣的靜態類, 別看只是自加, 但是可以實現類似c++中枚舉的效果, 十分方便
package Zszen.Nothing.core.notify {import flash.utils.Dictionary;/*** ...* @author Zszen John*/public class NotificationCenter {private static var _instance:NotificationCenter;private static var _suffix_prepare:String="_suffix_prepare"private static var _suffix_after:String="_suffix_after"public static var isDebug:Boolean = true;private var pool:Dictionary = new Dictionary();public static function defaultCenter():NotificationCenter {if (_instance == null) {if(isDebug)trace("[notify center]:inited")_instance = new NotificationCenter(new Single)}return _instance;}public static function Notify_Prepare(name:String):String {return name+_suffix_prepare}public static function Notify_After(name:String):String {return name+_suffix_after}public function NotificationCenter(single:Single) {}/public function postNotification(name:String, object:Object, userInfo:Object = null):void {if (pool[name]==null) {trace("[notify center]:rejected " + name)return;}var poolSub:Dictionary;var key:Object;var notify:Notification;//preparepoolSub = pool[name + _suffix_prepare];if (poolSub != null) {if(isDebug)trace("[notify center]:posted "+name+_suffix_prepare)for (key in poolSub) {notify = Notification(key)notify.objectPoster = objectnotify.userInfo = userInfo;notify.runAndClearPoster();}}//doingpoolSub = pool[name];if(isDebug)trace("[notify center]:posted "+name)for (key in poolSub) {notify = Notification(key)notify.objectPoster = objectnotify.userInfo = userInfo;notify.runAndClearPoster();}//afterpoolSub = pool[name + _suffix_after];if (poolSub!=null) {if(isDebug)trace("[notify center]:posted "+name+_suffix_after)for (key in poolSub) {notify = Notification(key)notify.objectPoster = objectnotify.userInfo = userInfo;notify.runAndClearPoster();}}}public function addObserverPrepare(name:String, object:Object, selector:Function):void {addObserver(name + _suffix_prepare, object, selector);}public function addObserverAfter(name:String, object:Object, selector:Function):void {addObserver(name + _suffix_after, object, selector);}public function addObserver(name:String, object:Object, selector:Function):void {if (hasSameObserver(name, object)) {if(isDebug)trace("[notify center]:addObserver same ",name,object)return;}if (pool[name]==null) {pool[name]=new Dictionary()}if (selector.length!=1) {throw "[notify center]:addObserver selector arguments length must be one and Notification Class"}var notify:Notification = new Notification(name, object, selector);pool[name][notify] = true;}private function hasSameObserver(name:String, object:Object):Boolean {var selector:Function = nullvar poolSub:Dictionary = pool[name];for (var key:Object in poolSub) {var notify:Notification = Notification(key)if (notify.object == object) {if (selector != null) {if (notify.selector == selector) {return true;break;}}else {return true;break;}}}return false;}public function hasObserver(object:Object):Boolean {if(isDebug)trace("[notify center]:check "+object)outloop:for (var name:String in pool) {var poolSub:Dictionary = pool[name];for (var key:Object in poolSub) {var notify:Notification = Notification(key)if (notify.object == object) {return true;}}}return false;}private function removeObserverWithSelector(object:Object, selector:Function = null):void {outloop:for (var name:String in pool) {var poolSub:Dictionary = pool[name];for (var key:Object in poolSub) {var notify:Notification = Notification(key)if (notify.object == object) {if (selector!=null) {if (notify.selector==selector) {trace("[notify center]:remove "+object)notify.clear()poolSub[notify] = null;delete poolSub[notify];break outloop;}}else {trace("[notify center]:remove "+object)notify.clear()poolSub[notify] = null;delete poolSub[notify];break outloop;}}}}}public function removeObserver(object:Object):void {removeObserverWithSelector(object,null)}public function removeAllObserver():void {if(isDebug)trace("[notify center]:remove all")for (var name:String in pool) {var poolSub:Dictionary = pool[name];for (var key:Object in poolSub) {var notify:Notification = Notification(key)notify.clear()poolSub[notify] = null;delete poolSub[notify];}pool[name] = null;delete pool[name];}}public function toString():String {var arr:Array = [];for (var name:String in pool) {var poolSub:Dictionary = pool[name];for (var key:Object in poolSub) {var notify:Notification = Notification(key)arr.push(notify)}}return arr.join("\n");}}}
class Single{}
使用方法也很簡單:
發出通知: (notifyinfo只是一個自定義的靜態字符串集而已, 第二個參數傳遞發送事件的對象, 也就是自身, 也可以是其他對象, 第三個參數是發送的參數, obj發送十分方便
NotificationCenter.defaultCenter().postNotification(NotifyInfo.XML_LOAD_COM,this,{data:data,path:xl.url})
接收通知:(結構類似, 第二個參數是接收對象, 第三個是接收的方法)
NotificationCenter.defaultCenter().addObserver(NotifyInfo.XML_LOAD_COM, this, onN)
處理通知: (這里會接收到nc的原子類 notification, 他存儲著接收者, 通知名稱, 發送者, 發送參數, 如果需要做復雜處理, 可以判斷接受者和發送者)?
private function onN(n:Notification):void {switch (n.name) {case NotifyInfo.<span style="font-family: Arial, Helvetica, sans-serif;">XML_LOAD_COM</span><span style="font-family: Arial, Helvetica, sans-serif;">:</span>
<span style="white-space:pre"> </span>//deal<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>}
}
整體這樣用了很久, 穩定性肯定是沒有問題的, 如果存在什么隱含問題, 請熱心給我留言
總結
以上是生活随笔為你收集整理的fd开发website小技巧的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。