像下面这样的东西对我有用,你可以从包括孩子在内的任何地方拖动容器,我添加了一个抑制点击标志来防止点击,如果容器是从孩子身上拖动的,你可能会使用或不使用它,这取决于你的应用程序。
package
{
public class Main extends Sprite
{
private var container:Sprite = new Sprite();
private var suppressClick:Boolean = false;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
container.graphics.beginFill(0xff0000, 1);
container.graphics.drawRect(0, 0, 500, 500);
container.graphics.endFill();
addChild(container);
container.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
var child:Sprite = new Sprite();
child.graphics.beginFill(0x00ff00, 1);
child.graphics.drawRect(0, 0, 50, 50);
child.graphics.endFill();
child.addEventListener(MouseEvent.CLICK, onChildClick);
child.x = 100;
child.y = 100;
container.addChild(child);
}
private function onMouseDown(e:MouseEvent):void
{
suppressClick = false;
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
container.startDrag();
}
private function onMouseMove(e:MouseEvent):void
{
suppressClick = true;
}
private function onMouseUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
container.stopDrag();
}
private function onChildClick(e:MouseEvent):void
{
if(!suppressClick)
trace("child clicked");
}
}
}