WEB开发网
开发学院图形图像Flash Flash AS3教程:Level2D类 阅读

Flash AS3教程:Level2D类

 2008-06-05 11:21:58 来源:WEB开发网   
核心提示: Level2D源代码:CODE:package index.com.truss{import flash.events.EventDispatcher;import flash.events.IEventDispatcher;import flash.events.Event;import

Level2D源代码:

CODE:

package index.com.truss{
    
    import flash.events.EventDispatcher;
    import flash.events.IEventDispatcher;
    import flash.events.Event;
    import flash.display.DisplayObjectContainer;
    import flash.display.DisplayObject;
    
    import index.com.events.Level2DEvent;
    
    public class Level2D implements IEventDispatcher{
        
        private var dispatcher:EventDispatcher;
        private var _range:DisplayObjectContainer;
        
        public function Level2D(range_:DisplayObjectContainer){
            dispatcher = new EventDispatcher(this);
            _range = range_;
        }
        
        //重排
        public function reset():Array{
            var tmpAr:Array = getDisplay();
            var getYAr:Array = getDisplay("y");
            var addYAr:Array = getYAr.concat().sort(16);
            
            if(getYAr.toString() != addYAr.toString()){
                inOrder(tmpAr.sortOn("y",16));
                
                //发布层级改变事件
                dispatcher.dispatchEvent(new Level2DEvent(Level2DEvent.LEVEL_RESET));
            }
            
            getYAr = addYAr = null;
            return tmpAr;
        }
        
        //返回当前范围所有对象
        public function getDisplay(arg:Object = null):Array{
            var i:int;
            var tmpAr:Array = new Array;
            var num:int = _range.numChildren;
            if(arg == null){
                for(i = 0; i < num; i ++) tmpAr.push(_range.getChildAt(i));
            }else if(arg is String){
                for(i = 0; i < num; i ++) tmpAr.push(_range.getChildAt(i)[arg]);
            }else{
                for(i = 0; i < num; i ++){
                    var dis:DisplayObject = _range.getChildAt(i);
                    var isEnter:Boolean = true;
                    for(var index:String in arg){
                        if(dis[index] != arg[index]){
                            isEnter = false;
                            break;
                        }
                    }
                    if(isEnter) tmpAr.push(dis);
                    dis = null;
                }
            }
            return tmpAr;
        }
        
        //以特定方式遍历该范围的显示对象
        public function forEach(fun:Function,arg:Object = null):void{
            var tmpAr:Array = getDisplay(arg);
            for(var i:int = 0; i < tmpAr.length; i ++){
                var str:String = fun(tmpAr[i],i);
                if(str == "true") return;
            }
            tmpAr = null;
        }
        
        //给定一个排序数组,进行排序
        protected function inOrder(tmpAr:Array):void{
            for(var i:int = 0; i < tmpAr.length; i ++) _range.addChild(tmpAr[i] as DisplayObject);
        }
        
        //设置实时重排
        public function set isTime(_isTime:Boolean):void{
            if(_isTime) _range.addEventListener(Event.ENTER_FRAME,resetFun);
            else _range.removeEventListener(Event.ENTER_FRAME,resetFun);
        }
        
        //实时重排
        private function resetFun(e:Event):void{
            reset();
        }
        
        //返回操作区域
        public function get range():DisplayObjectContainer{
            return _range;
        }
        
        //侦听
        public function addEventListener(type:String,listener:Function,useCapture:Boolean = false,priority:int = 0,useWeakReference:Boolean = false):void{
            dispatcher.addEventListener(type,listener,useCapture,priority,useWeakReference);
        }
        
        //调度
        public function dispatchEvent(event:Event):Boolean{
            return dispatcher.dispatchEvent(event);
        }
        
        //是否含有侦听
        public function hasEventListener(type:String):Boolean{
            return dispatcher.hasEventListener(type);
        }
        
        //移除侦听
        public function removeEventListener(type:String,listener:Function,useCapture:Boolean = false):void{
            dispatcher.removeEventListener(type,listener,useCapture);
        }
        
        //检查侦听
        public function willTrigger(type:String):Boolean{
            return dispatcher.willTrigger(type);
        }
    }
}

Level2DEvent类源代码:

CODE:

package index.com.events{
    
    import flash.events.Event;
    
    public class Level2DEvent extends Event{
        
        public static const LEVEL_RESET:String = "levelReset";
        
        public function Level2DEvent(type:String){
            super(type);
        }
    }
}

上一页  1 2 3 

Tags:Flash AS 教程

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接