简体   繁体   中英

Dynamic MC's path with localToGlobal Problems

I'm adding several instances of an MC (bread_mc) into a container MC (wall_mc). wall_mc has been added to a shop class and that is where i'm called the rest of my functions from. Also the wall_mc is scrolled left and right with mouseX & mouseY values. I've set up a function to add the bread_mc to the wall at different x/y positions.

Function call:

fillShelves(bread_mc,Bread,600,200);

Function:

 function fillShelves(productMc:MovieClip,className:Class,productX:Number, productY:Number) {

        newProduct = new className();
        newProduct.x=productX;
        newProduct.y=productY;
        shelf_mc.addChildAt(newProduct,1);

        newProduct.addEventListener(MouseEvent.MOUSE_DOWN, dragProduct, false, 0, true);

    }

I've added a drag/drop where the bread_mc will snap back to it's shelf x/y position. However i'd like that when user MOUSE_DOWN on bread_mc it is added to the highest depth so, while dragged, it stays infront of other mc's (a person with a shopping cart for example).

My problem is, whenever I call the function dragProduct();

function dragProduct(e:MouseEvent):void {

    currMC = (e.target as MovieClip);
    this.addChild(currMC); 
    stage.addEventListener(MouseEvent.MOUSE_UP, dropProduct);

    }

The damn bread_mc I click on moves to the right by the same amount that I've scrolled the wall_mc. I have tried forcing the currMC to stick with the mouseX/Y but that would need an ENTER_FRAME function and that seems so convoluted.

I researched and found localToGlobal. Methods simliar to this:

point = new Point(currMC.x, currMC.y);
    currMC.localToGlobal(point);
    trace(point.x);

I really don't even know if i'm using it right, But I think my problem lies in the fact that I need the currMC path to travel up the display list tree and get the global stage X/Y position. I'm not too sure how to implement a dynamic path with the localToGlobal method.

Any help would be appreciated. Even if it's go here>read this. Thanks

Your dragged bread_mc moveclip moves because it is being move to a different container that has a different origin. In your fillShelves function you are adding bread_mc as a child of shelf_mc . But in the dragProduct function you are adding it to this . Just try adding bread_mc to shelf_mc rather than this . And forget about all the localToGlobal stuff.

When you call addChild , it will bring the target to the front of all its siblings.

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