重要地方说明:
1、获取当前点击元件的实例:e.target.name。感谢etall跟蓝色海洋。。。要想获取当前点击元件就得e.target。
2、将元件调到最高级。。。
my_Spr.setChildIndex(DisplayObject(e.target),my_Spr.numChildren-1);
在使用DisplayObject之前,需要导入该类。import flash.display.DisplayObject;
3、关于拖曳其实比较简单。主要是获取当前点击的这个比较难。
1、在FLA中建四个球,分别实例"ball1"、"ball2"、"ball3"、"ball4"。
2、main.as:
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.display.DisplayObject;
/**
* ...
* @author 沙子 QQ:8286659 http://www.taoshaw.com
*/
public class main extends Sprite
{
//定义四个球
private var my_ball1:ball1;
private var my_ball2:ball2;
private var my_ball3:ball3;
private var my_ball4:ball4;
private var my_Spr:Sprite;
public function main()
{
_showball1();
_showball2();
_showball3();
_showball4();
my_Spr = new Sprite();
//将第一个球显示在第一层,
my_Spr.addChildAt(my_ball1, 0)
//将第二个球显示在第二层,
my_Spr.addChildAt(my_ball2, 1)
//将第一个球显示在第三层,
my_Spr.addChildAt(my_ball3, 2)
//将第一个球显示在第一层,
my_Spr.addChildAt(my_ball4, 3)
my_ball1.addEventListener(MouseEvent.MOUSE_DOWN, startDrags)
my_ball1.addEventListener(MouseEvent.MOUSE_UP, stopDrags)
my_ball2.addEventListener(MouseEvent.MOUSE_DOWN, startDrags)
my_ball2.addEventListener(MouseEvent.MOUSE_UP, stopDrags)
my_ball3.addEventListener(MouseEvent.MOUSE_DOWN, startDrags)
my_ball3.addEventListener(MouseEvent.MOUSE_UP, stopDrags)
my_ball4.addEventListener(MouseEvent.MOUSE_DOWN, startDrags)
my_ball4.addEventListener(MouseEvent.MOUSE_UP, stopDrags)
//此句非要不可,否则出错.
addChild(my_Spr)
}
private function stopDrags(e:MouseEvent):void
{
e.target.stopDrag();
}
private function startDrags(e:MouseEvent):void
{
e.target.startDrag();
//将当前点击的元件的层级设置为my_Spr所有子项目数的最高层.
my_Spr.setChildIndex(DisplayObject(e.target),my_Spr.numChildren-1)
}
private function _showball4():void
{
my_ball1 = new ball1();
my_ball1.x = 100;
my_ball1.y = 200;
}
private function _showball3():void
{
my_ball2 = new ball2();
my_ball2.x = 150;
my_ball2.y = 200;
}
private function _showball2():void
{
my_ball3 = new ball3();
my_ball3.x = 200;
my_ball3.y = 200;
}
//显示第一个球体
private function _showball1():void
{
my_ball4 = new ball4();
my_ball4.x = 250;
my_ball4.y = 200;
}
}
}