Flash AS3教程:Level2D类

前面学习了Flash AS3教程:ApplySocket类, http://www.webjx.com/files/media/Level2D.swf 该类是对于同平面物体的深度简单的自动管理类,只是做到了简单的虚拟社区等,人物,建筑物等深度排列,太复杂的估计就搞不定了,但是可以继承这个类继续定义 index.com.main.Leval

前面学习了Flash AS3教程:ApplySocket类,
https://www.0133.cn/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 }else if(arg is String){
for(i = 0; i }else{
for(i = 0; 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 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 }

//设置实时重排
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);
}
}
}

以上就是Flash AS3教程:Level2D类的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Flash教程