0

我的目标是在事件上生成一个圆圈,并在发生拖动时MOUSE_WHEEL移动所有MOUSE_DOWN圆圈,如 和 检测到的MOUSE_UP。我通过将每个Sprite创建的元素添加到一个数组中来完成此操作,并在鼠标向上/向下时迭代它。注意:Node只是Sprite类型的扩展。

但是,由于某种原因,只有最近绘制Sprite的数组被移动。任何想法为什么?

我的画布课:

    public function Canvas() {
        trace("Starting it");

        const background:Sprite = new Sprite();
        background.graphics.beginFill(0x00000000);
        background.graphics.drawRect(0, 0, this.stage.stageWidth, this.stage.stageHeight);
        background.graphics.endFill();
        addChild(background);

        background.addEventListener(MouseEvent.MOUSE_WHEEL, createNode);
        background.addEventListener(MouseEvent.MOUSE_DOWN, startObjectMove);
        background.addEventListener(MouseEvent.MOUSE_UP, endObjectMove);


        mNodeList = new Array();
    }
...
}

我的 startObjectMove 和 endObjectMove 方法:

        public function startObjectMove(pEvent:MouseEvent) : void {
        trace("Starting drag...");
        trace("There are " + mNodeList.length + " in list");
        for (var i:int = 0; i < mNodeList.length; i++) {
            var node:Node;
            node = Node(mNodeList[i]);
            node.startMove(pEvent);
        }
    }
    public function endObjectMove(pEvent:MouseEvent) : void {
        trace("Ending drag...");
        trace("There are " + mNodeList.length + " in list");
        for (var i:int = 0; i < mNodeList.length; i++) {
            var node:Node;
            node = Node(mNodeList[i]);
            node.endMove(pEvent);
        }
    }

endMoveand方法startMove只是调用this.startDrag()and 。this.endDrag()

4

1 回答 1

1

您一次只能在一个对象上使用 startDrag 和 stopDrag。

您需要在舞台本身上监听鼠标按下事件 - 当用户单击时,存储每个节点的初始位置(在 Node 类中创建两个公共变量/属性),并记录鼠标从哪里开始。

然后,每当鼠标移动直到它被释放时,计算它从鼠标向下移动了多远,并将这个量添加到每个节点的初始位置以设置它的位置。

你需要做这样的事情:

var initX:int;//Variables to store the mouse position on click:
var initY:int;

stage.addEventListener(MouseEvent.MOUSE_DOWN):void {

    initX = stage.mouseX;//Store the starting mouse position
    initY = stage.mouseY;


    //Store the node positions:
    for(var i:int = 0; i < mNodeList.length; i++){
        var node:Node = Node(mNodeList[i]);
        node.startX = node.x;
        node.startY = node.y;
    }

    //Listen for mouse move events
    stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMove);
}


stage.addEventListener(MouseEvent.MOUSE_UP):void {
    //Stop listening for mouse move events
    stage.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMove);
}



//MouseMove event handler:

function mouseMove(e:MouseEvent):void{
        for(var i:int = 0; i < mNodeList.length; i++){
                var node:Node = Node(mNodeList[i]);

                // Position each as amount the mouse has moved 
                // to each node's initial position:

                node.x = (stage.mouseX-initX) + node.startX;
                node.y = (stage.mouseY-initY) + node.startY;
        }

}
于 2013-04-01T02:50:23.367 回答