CODE:import index.com.main.Level2D;
Level2D源代码:
import index.com.events.Level2DEvent;
var l2:Level2D = new Level2D(this);
l2.addEventListener(Level2DEvent.LEVEL_RESET,levelResetFun);
l2.isTime = true;
function levelResetFun(e:Level2DEvent){
trace(l2.getDisplay("x"));//获取当前所有对象的x属性
trace(l2.getDisplay({y:0,x:0}));//获取当前所有对象,xy属性都等于0的对象
/**
*输出:
*137,0,229
*[object MovieClip]
*/
}
mc1.addEventListener(MouseEvent.MOUSE_DOWN,funa);
mc2.addEventListener(MouseEvent.MOUSE_DOWN,funa);
mc3.addEventListener(MouseEvent.MOUSE_DOWN,funa);
mc1.addEventListener(MouseEvent.MOUSE_UP,funb);
mc2.addEventListener(MouseEvent.MOUSE_UP,funb);
mc3.addEventListener(MouseEvent.MOUSE_UP,funb);
function funa(e:MouseEvent):void{
e.target.startDrag();
}
function funb(e:MouseEvent):void{
e.target.stopDrag();
}
CODE:package index.com.truss{
Level2DEvent类源代码:
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);
}
}
}
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);
}
}
}
IT时代网(关注微信公众号ITtime2000,定时推送,互动有福利惊喜)所有原创文章版权所有,未经授权,转载必究。
创客100创投基金成立于2015年,直通硅谷,专注于TMT领域早期项目投资。LP均来自政府、互联网IT、传媒知名企业和个人。创客100创投基金对IT、通信、互联网、IP等有着自己独特眼光和丰富的资源。决策快、投资快是创客100基金最显著的特点。
小何
小何
小何
小何