只要添加到显示列表中,就算它的大小或者宽高为0。它也会占据整个层,不用填充其中内容,一样也可以监听到鼠标事件。下面的代码添加到自动分层
private var canvas:UIComponent = new UIComponent();
private var layer1:Sprite = new Sprite();
private var layer2:Sprite = new Sprite();
private var layer3:Sprite = new Sprite();
private var e1:Sprite = new Sprite();
private var e11:Sprite = new Sprite();
private var e2:Sprite = new Sprite();
private var e3:Sprite = new Sprite();
protected function button2_clickHandler(event:MouseEvent):void
{
drawElement();
canvas.addChild(layer1);
canvas.addChild(layer2);
canvas.addChild(layer3);
layer1.addChild(e1);
layer1.addChild(e11);
layer2.addChild(e2);
layer3.addChild(e3);
this.addElement(canvas);
AddEvent();
}
public function drawElement():void
{
e1.graphics.beginFill(0xff0000);
e1.graphics.drawCircle(100,100,10);
e1.graphics.endFill();
e11.graphics.beginFill(0xd3d3d3);
e11.graphics.drawCircle(100,100,10);
e11.graphics.endFill();
e2.graphics.beginFill(0x00ff00);
e2.graphics.drawCircle(100,100,10);
e2.graphics.endFill();
e3.graphics.beginFill(0x0000ff);
e3.graphics.drawCircle(100,100,10);
e3.graphics.endFill();
}
public function AddEvent():void
{
canvas.addEventListener(MouseEvent.MOUSE_DOWN,onStartDrag);
canvas.addEventListener(MouseEvent.MOUSE_UP,onStopDrag);
}
public function onStartDrag(e:MouseEvent):void
{
Sprite(e.target).startDrag();
}
public function onStopDrag(e:MouseEvent):void
{
Sprite(e.target).stopDrag();
}