拖动容器的子容器时 mouseX/Y 混乱

发布于 2024-11-09 11:38:47 字数 427 浏览 0 评论 0原文

我有一个图像网格,它们被添加到类中的图像容器(Sprite)中。 当我单击按钮时,图像容器会补间(缩放)为 0.2 现在我想开始拖动图像。鼠标按下时,我添加一个 EnterFrame 事件:

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

}

不幸的是,图像永远不会在鼠标位置上,但与鼠标指针的偏移量会增加/减少。 另一种方法 startDrag/stopDrag 效果很好,但我仍然需要 mouseX/mouseY 将图像放置在网格上...... 我也尝试过parent.mouseX,但没有成功。 为什么会发生这种情况?我认为 mouseX/mouseY 始终取决于舞台尺寸。

I've got a grid of images which are added to a imagecontainer(Sprite) in a class.
When I click a button, the imagecontainer gets tweened(scaled) to 0.2
Now I would like to start dragging around the images. on mouse-down I add an enterFrame Event:

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

}

Unfortunately the image is never on the mouseposition but has a increasing/decreasing offset to the mouse pointer.
The alternative, startDrag/stopDrag works perfectly, but I still need the mouseX/mouseY for placing the image on a grid…
I tried also parent.mouseX, no luck with that.
Why is this happening? I thought mouseX/mouseY is always depending on the stage-dimension.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

_失温 2024-11-16 11:38:48

如果您想获取相对于舞台上鼠标位置的 mouseXmouseY 那么为什么不使用:

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);
        }
    }
}

If you want to get the mouseX and mouseY relative to the mouse position on the stage then why don't you use:

stage.mouseX,
stage.mouseY

Also, if you scale something up (let's say to a total size of 200%), then 50 pixels across on the stage is actually 25 pixels across in the container that has been scaled.

Use this as a document class to see what I mean:

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);
        }
    }
}
七秒鱼° 2024-11-16 11:38:48

如果您仍在寻找这个,也许您可​​以使用 startDrag();和 stopDrag();
用于拖动图像。

像这样:

    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();
    }

更新:您可以像这样设置你的函数:

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
    }
}

If you are still looking for this maybe you can use startDrag(); and stopDrag();
for dragging 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 set ur function like this:

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
    }
}
没有你我更好 2024-11-16 11:38:47

你试过吗:

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

}

have you tried:

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文