AS3中的深度管理非常简单。主要用到两种方法:getChildIndex()和setChildIndex()。
深度管理的操作有4种:上移,下移,置顶,置底。 上移操作,只需通过getChildIndex()方法获得当前对象的索引位置i,将i的值加1,然后通过setChildIndex()方法将当前对象放到新的索引位置。
下移操作同理。
置顶操作,只需将当前对象放到索引位置为numChildren-1的位置即可。
置底操作只需将当前对象放到索引位置为0的位置。
下面是我写的一个试验深度管理的代码。
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
doubleClickEnabled="true"
windowComplete="initApp()">
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.controls.Button;
private var s1:Sprite=new Sprite();
private var s2:Sprite=new Sprite();
private var s3:Sprite=new Sprite();
private var s4:Sprite=new Sprite();
private var array:Array=new Array();
private var oldX:Number;
private var oldY:Number;
private var mX:Number;
private var mY:Number;
private var selectBtnArray:Array=new Array();
private var upBtn:Button;
private var downBtn:Button;
private var zhidingBtn:Button;
private var zhidiBtn:Button;
private function initApp():void
{
stage.displayState=StageDisplayState.FULL_SCREEN_INTERACTIVE;
init();
}
private function init():void
{
creatSprite(s1, 0x000000);
creatSprite(s2, 0x0000ff);
creatSprite(s3, 0x2f4f4f);
creatSprite(s4, 0xffa550);
array.push(s1);
array.push(s2);
array.push(s3);
array.push(s4);
for (var i:int=0; i < 4; i++)
{
(array[i] as Sprite).x=i * 170;
(array[i] as Sprite).y=130;
ui.addChild(array[i]);
}
creatButton();
stage.addEventListener(MouseEvent.CLICK, stageClickHandler);
}
/**
*创建矩形对象
*/
private function creatSprite($sprite:Sprite, $color:uint):void
{
$sprite.graphics.beginFill($color);
$sprite.graphics.drawRect(0, 0, 150, 100);
$sprite.graphics.endFill();
$sprite.addEventListener(MouseEvent.MOUSE_DOWN,downHandler);
}
/**
* 矩形的拖拽操作
*/
private function downHandler(e:Event):void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, upHandler);
if (selectBtnArray.length > 0)
{
selectBtnArray.splice(0, selectBtnArray.length);
selectBtnArray.push(e.target);
}
else
{
selectBtnArray.push(e.target);
}
mX=mouseX;
mY=mouseY;
oldX=(e.target as Sprite).x;
oldY=(e.target as Sprite).y;
}
private function moveHandler(e:Event):void
{
(selectBtnArray[0] as Sprite).x=mouseX - (mX - oldX);
(selectBtnArray[0] as Sprite).y=mouseY - (mY - oldY);
}
private function upHandler(e:Event):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
stage.removeEventListener(MouseEvent.MOUSE_UP, upHandler);
}
private function doubleClick(e:Event):void
{
if (stage.displayState != StageDisplayState.FULL_SCREEN_INTERACTIVE)
{
stage.displayState=StageDisplayState.FULL_SCREEN_INTERACTIVE;
}
else
{
stage.displayState=StageDisplayState.NORMAL;
}
}
/**
* 创建上移,下移,置顶,置底按钮
*/
private function creatButton():void
{
upBtn=new Button();
upBtn.label="上移";
upBtn.x=30;
upBtn.width=80
upBtn.height=30;
upBtn.addEventListener(MouseEvent.CLICK, upBtnHandler);
ui.addChild(upBtn);
downBtn=new Button();
downBtn.label="下移";
downBtn.x=150;
downBtn.width=80;
downBtn.height=30;
downBtn.addEventListener(MouseEvent.CLICK, downBtnHandler);
ui.addChild(downBtn);
zhidingBtn=new Button();
zhidingBtn.label="置顶";
zhidingBtn.x=300;
zhidingBtn.width=80
zhidingBtn.height=30;
zhidingBtn.addEventListener(MouseEvent.CLICK, zhidingBtnHandler);
ui.addChild(zhidingBtn);
zhidiBtn=new Button();
zhidiBtn.label="置底";
zhidiBtn.x=400;
zhidiBtn.width=80
zhidiBtn.height=30;
zhidiBtn.addEventListener(MouseEvent.CLICK, zhdiBtnHandler);
ui.addChild(zhidiBtn);
}
private function upBtnHandler(e:Event):void
{
if (selectBtnArray.length > 0)
{
var j:int=ui.getChildIndex(selectBtnArray[0]);
if (j < array.length - 1)
{
if (selectBtnArray.length > 0)
{
var sprite:Sprite=Sprite(selectBtnArray[0]);
ui.setChildIndex(sprite, j + 1);
}
}
}
}
private function downBtnHandler(e:Event):void
{
if (selectBtnArray.length > 0)
{
var j:int=ui.getChildIndex(selectBtnArray[0]);
if (j > 0)
{
var sprite:Sprite=Sprite(selectBtnArray[0]);
ui.setChildIndex(sprite, j - 1);
}
}
}
private function zhidingBtnHandler(e:Event):void
{
if (selectBtnArray.length > 0)
{
var sprite:Sprite=Sprite(selectBtnArray[0]);
ui.setChildIndex(sprite, array.length - 1);
}
}
private function zhdiBtnHandler(e:Event):void
{
if (selectBtnArray.length > 0)
{
var sprite:Sprite=Sprite(selectBtnArray[0]);
ui.setChildIndex(sprite, 0);
}
}
private function stageClickHandler(e:Event):void
{
if (String(e.target) == 'ShenDuGuanLi0.WindowedApplicationSkin2')
{
if (selectBtnArray.length > 0)
{
selectBtnArray.splice(0, selectBtnArray.length);
}
}
}
]]>
</fx:Script>
<mx:UIComponent id="ui"/>
</s:WindowedApplication>