MouseX / Y confusion when dragging a child from a container

I have a grid of images that are added to imagecontainer (Sprite) in the class. When I click the button, the imagecontainer gets tween (scaled) to 0.2. Now I would like to start dragging the images. on hover, I add the EnterFrame event:

function onEnterFrame(e:Event):void
{
  imagecontainer.image.x = this.mouseX;
  imagecontainer.image.y = this.mouseY;

}

Unfortunately, the image is never located on the mouse pointer, but the offset increases / decreases with the mouse pointer. Alternatively, startDrag / stopDrag works fine, but I still need mouseX / mouseY to place the image on the grid ... I also tried parent.mouseX, no luck. Why is this happening? I thought mouseX / mouseY always depends on the dimension of the scene.

+3
source share
3 answers

:

function onEnterFrame(e:Event):void
{
  imagecontainer.image.x = stage.mouseX;
  imagecontainer.image.y = stage.mouseY;

}
+3

mouseX mouseY stage, :

stage.mouseX,
stage.mouseY

, - (, 200%), 50 25 , .

, , :

package
{
    import flash.display.MovieClip;
    import flash.events.Event;

    public class Main extends MovieClip
    {
        private var _box:MovieClip;

        public function Main()
        {
            addEventListener(Event.ENTER_FRAME, _move);

            _box = new MovieClip();
            _box.scaleX = _box.scaleY = 2;

            addChild(_box);
        }

        private function _move(e:Event):void
        {
            trace("stage: " + stage.mouseX + ", " + stage.mouseY);
            trace("box: " + _box.mouseX + ", " + _box.mouseY);
        }
    }
}
+1

If you are still looking for this, perhaps you can use startDrag (); and stopDrag (); to drag and drop images.

like this:

    image.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownMC);
    image.addEventListener(MouseEvent.MOUSE_UP, onMouseUpMC);

    function onMouseDownMC(e:MouseEvent):void
    {
        e.currentTarget.startDrag(true);
    }
    function onMouseUpMC(e:MouseEvent):void
    {
        e.currentTarget.stopDrag();
    }

Update: you can install ur function as follows:

image.addEventListener(MouseEvent.MOUSE_DOWN, onMouseMC);
image.addEventListener(MouseEvent.MOUSE_UP, onMouseMC);

function onMouseMC(e:MouseEvent):void
{
    var type = e.type;
    if(type == MouseEvent.MOUSE_DOWN) /// u can use "mouseDown" accept  MouseEvent.MOUSE_DOWN 
    {
        e.currentTarget.startDrag(true);// if you set true its gonna drag the obj from center
    }
    else if(type == MouseEvent.MOUSE_UP)
    {
        e.currentTarget.stopDrag();  //u can use "mouseUp" accept  MouseEvent.MOUSE_UP
    }
}
0
source

All Articles