AS3 - Child使用startDrag移动父级

时间:2009-11-08 17:48:54

标签: flash actionscript-3 drag-and-drop parent-child

我有一个MovieClip实例,可以使用startDrag()和stopDrag()在舞台上移动。该实例还有一些使用addChild()的子MovieClip。父母在拖动时会移动孩子,这很好。子节点有自己的startDrag()和stopDrag(),它们只应该应用于子对象,但它也会移动父节点和其他子节点。单击子项时,将调用子项的MouseEvent,但父项也是如此。

public class Component extends MovieClip {
    private var nodes_array:Array = new Array();

    public function Component() {
        x = 60;
        y = 100;

        nodes_array.push(addChild(new Node(50, 50)));
        nodes_array.push(addChild(new Node(150, 150)));

        addEventListener(MouseEvent.MOUSE_DOWN, startDraggingComponent);
        addEventListener(MouseEvent.MOUSE_UP, stopDraggingComponent);
    }
    private function startDraggingComponent(me:MouseEvent):void {
        this.startDrag();
    }
    private function stopDraggingComponent(me:MouseEvent):void {
        this.stopDrag();
    }


    public class Node extends MovieClip {

    public function Node(x:int, y:int) {
        this.x = x;
        this.y = y;

        addEventListener(MouseEvent.MOUSE_DOWN, startDraggingNode);
        addEventListener(MouseEvent.MOUSE_UP, stopDraggingNode);
    }
    private function startDraggingNode(me:MouseEvent):void {
        this.startDrag();
    }
    private function stopDraggingNode(me:MouseEvent):void {
        this.stopDrag();
    }

1 个答案:

答案 0 :(得分:3)

在Node类侦听器中,您需要调用e.stopImmediatePropagation();。这样可以防止事件冒泡到其父级。