【问题标题】:AS3 - Child moves parent using startDragAS3 - 子级使用 startDrag 移动父级
【发布时间】:2009-11-08 17:48:54
【问题描述】:

我有一个可以使用 startDrag() 和 stopDrag() 在舞台上移动的 MovieClip 实例。该实例还有一些使用 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();
    }

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-09
      • 2019-12-15
      • 1970-01-01
      • 2011-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多