Actionscript 3 在Actionscript 3中拖动精灵数组

Actionscript 3 在Actionscript 3中拖动精灵数组,actionscript-3,Actionscript 3,我的目标是在鼠标滚轮事件上生成一个圆圈,并在发生拖动时移动所有圆圈,如鼠标向下和鼠标向上检测到的。我将创建的每个Sprite添加到一个数组中,并在鼠标上下移动时对其进行迭代。注意:Node只是Sprite类型的扩展 但是,由于某些原因,仅移动阵列中最近绘制的精灵。你知道为什么吗 我的画布类: public function Canvas() { trace("Starting it"); const background:Sprite = new Spr

我的目标是在
鼠标滚轮
事件上生成一个圆圈,并在发生拖动时移动所有圆圈,如
鼠标向下
鼠标向上
检测到的。我将创建的每个
Sprite
添加到一个数组中,并在鼠标上下移动时对其进行迭代。注意:
Node
只是
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);
        }
    }
公共函数startObjectMove(pEvent:MouseEvent):无效{
跟踪(“开始拖动…”);
跟踪(“列表中有“+mNodeList.length+”);
for(变量i:int=0;i

endMove
startMove
方法只需调用
this.startDrag()
this.endDrag()

一次只能对一个对象使用startDrag和stopDrag

您需要在stage本身上侦听鼠标按下事件-当用户单击时,存储每个节点的初始位置(在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;
        }

}
var initX:int//用于在单击时存储鼠标位置的变量:
变量:int;
stage.addEventListener(MouseEvent.MOUSE_DOWN):无效{
initX=stage.mouseX;//存储开始的鼠标位置
initY=stage.mouseY;
//存储节点位置:
for(变量i:int=0;i
非常感谢,我不知道您不能将其用于多个对象!我希望它能工作,如果你想再看的话,我已经修改了很多了!