https://www.jb51.net/files/media/level2d.swf
该类是对于同平面物体的深度简单的自动管理类,只是做到了简单的虚拟社区等,人物,建筑物等深度排列,太复杂的估计就搞不定了,但是可以继承这个类继续定义
index.com.main.leval2d类:
代码:public class level2d implements ieventdispatcher
现实:ieventdispatcher接口
对同一容器中的物体以y坐标为基准进行自动深度管理
构造函数:
public function level2d(range_:displayobjectcontainer)
参数表示要对哪个容器中的对象进行自动深度管理
手动排列深度 方法:
public function reset():array
返回排列后的对象数组,如果层级无改变,则返回当前对象数组
层级改变将调度事件level2devent.level_reset
获取对象列表 方法:
public function getdisplay(arg:object = null):array
获取当前管理容器中,以arg方式过滤后的所有对象
若arg为空,则返回当前容器中所有对象列表
若arg为字符串,则返回当前容器中所有对象的arg属性列表
若arg为object,则返回当前容器中,符合arg所有属性的对象列表
遍历 方法:
public function foreach(fun:function,arg:object = null):void
以某种方式遍历所有对象,并且有arg方式进行过滤遍历
第一个参数,在遍历的时候,每遍历到一个对象,都会调用fun函数,将显示对象和排列位置传出去,例如:
function callback(_display:displayobject,index:int){}
第二个参数,等同于getdisplay中arg参数
私有继承方法:
protected function inorder(tmpar:array):void
以某种特定的数组进行深度改变
该方法必须继承该类,它的子集便可以当作私有属性来调用该方法,外部不可见!
设置即时属性:(只写)
public function set istime(_istime:boolean):void
当参数为true,则表示层级在可以改变的时候,会自动进行改变
当参数为false,则表示需要手动调用reset方法,才能改变深度排列
range属性:(只读)
public function get range():displayobjectcontainer
当实例化管理容器后,就无法修改该属性了,只有通过调用range来读取属性了
举例:
上面那个flash的源代码,三个小方块都是可以拖拽的,
code:
import index.com.main.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();
}
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);
}
}
}