0

我有一个容器,其中包含需要向他们注册 CLICK 事件的孩子。容器是可拖动的,孩子不应该干扰拖动或被它触发。

我很难弄清楚如何实现这一目标。任何帮助表示赞赏。

这是我能做到的最接近的方法,如果对象已被移动,则删除任何点击处理程序。虽然这看起来很乱

box.addEventListener(MouseEvent.MOUSE_DOWN, dragit);
box.circle.addEventListener(MouseEvent.CLICK, circleclick);


function dragit(e:MouseEvent) 
{
   box.startDrag();
   box.addEventListener(MouseEvent.MOUSE_UP, stopdrag);
}


function stopdrag(e:Event) 
{
if(box.x != 0)
    {
        box.circle.removeEventListener(MouseEvent.CLICK, circleclick);
    }
    box.stopDrag();
}

function circleclick(e:MouseEvent):void
{
trace('clicked');
}
4

1 回答 1

3

像下面这样的东西对我有用,你可以从包括孩子在内的任何地方拖动容器,我添加了一个抑制点击标志来防止点击,如果容器是从孩子身上拖动的,你可能会使用或不使用它,这取决于你的应用程序。

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");
        }

    }

}
于 2013-04-26T02:39:32.413 回答