简体   繁体   中英

Flash as3 mouse custom cursor nested in a movieclip

I have a parent MC (container), with 3 nested MCs inside (mc1, mc2, mc3). The "container" MC is positioned somewhere on the stage (not necessarily at 0,0) and has the registration point in the top left corner. When I drag one of the nested MCs (which have the registration point in the middle), I want the target MC to follow the mouse anywhere on the stage.

I want to do this without using startDrag() method. I know i have to set the coordinates of the target MC in the MOUSE_MOVE handler function based on the mouse position on the stage, something like...

container.mc1.addEventListener(MouseEvent.MOUSE_DOWN, drag);
function drag(e:MouseEvent):void{
    stage.addEventListener(MouseEvent.MOSUE_MOVE, move);
}
function move(e:MouseEvent):void{
     container.mc1.x = ??
     container.mc2.y = ??
}

How do I do that? I am very confused about the localToGlobal and globalToLocal methods.

看图片

You can also use mouseDown, etc. I sometimes find this more versatile than startDrag. The following locks the mc to the current offset of the mouse, but is easily modified:

private var drag_info;
mc.addEventListener("mouseDown", dragStart);

private function dragStart(event:MouseEvent):void {
    var who = event.target as MovieClip;
    drag_info = { offsetX:mouseX-who.x, offsetY:mouseY-who.y, who:who };
    stage.addEventListener("mouseMove",dragUpdate);
    stage.addEventListener("mouseUp",dragStop);
}
private function dragUpdate(event:MouseEvent) {
    var who = drag_info.who;
    who.x = mouseX - drag_info.offsetX;
    who.y = mouseY - drag_info.offsetY;
}
private function dragStop(event:MouseEvent) {
    stage.removeEventListener("mouseMove",dragUpdate);
    stage.removeEventListener("mouseUp",dragStop);
}

I wanted to say this as a comment, but my low reputation won't let me just yet.

As Vesper mentioned, why don't you use startDrag()? It seems like that's exactly what you want/need. And as for locking the center point to the mouse, startDrag() has that covered:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Sprite.html#startDrag%28%29

Just use the first parameter of the startDrag() method as 'true', to use center locking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM