为什么 startDrag() 和 stopDrag() 不影响 x,y 坐标?

发布于 2024-08-24 14:17:40 字数 2023 浏览 3 评论 0原文

当我使用 startDrag() 和 stopDrag() 拖动对象时,为什么它不影响对象的 x,y 坐标?

你可以运行这个例子并亲自看看 (移动圆圈并查看跟踪消息):

<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;

        private function mouseDown(event:MouseEvent):void {
            circle.startDrag(false, new Rectangle(0,0 , 400, 400));
        }


        private function mouseReleased(event:MouseEvent):void {
            circle.stopDrag();
            trace("ended drag X: " + circle.x + ", Y: " + circle.y);
        }


        protected function application1_creationCompleteHandler(event:FlexEvent):void
        {
            circle.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown) 
            circle.addEventListener(MouseEvent.MOUSE_UP, mouseReleased);
        }

    ]]>
</fx:Script>

<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<s:Rect id="target1" x="0" y="10" width="150" height="150">
    <s:fill>
        <s:SolidColor color="0xFF0000"/>
    </s:fill>
</s:Rect>

<s:Rect id="target2" x="200" y="10" width="150" height="150">
    <s:fill>
        <s:SolidColor color="0x00FF00"/>
    </s:fill>
</s:Rect>

<s:Graphic id="circle" x="200" y="200">
    <s:Ellipse height="100" width="250">
        <s:stroke>
            <s:SolidColorStroke color="0x000000" weight="2"/>
        </s:stroke>
        <s:fill>
            <s:RadialGradient>
                <s:entries>
                    <s:GradientEntry color="0x0056FF" ratio="0.00" alpha="0.5"/>
                    <s:GradientEntry color="0x00CC99" ratio="0.33" alpha="0.5"/>
                    <s:GradientEntry color="0xECEC21" ratio="0.66" alpha="0.5"/>
                </s:entries>
            </s:RadialGradient>
        </s:fill>
    </s:Ellipse>
</s:Graphic>

when I use startDrag() and stopDrag() to drag an object why doesn't it effect the x,y coordinates of the object?

you can run this example and see for yourself
(move the circle and look at the trace messages):

<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;

        private function mouseDown(event:MouseEvent):void {
            circle.startDrag(false, new Rectangle(0,0 , 400, 400));
        }


        private function mouseReleased(event:MouseEvent):void {
            circle.stopDrag();
            trace("ended drag X: " + circle.x + ", Y: " + circle.y);
        }


        protected function application1_creationCompleteHandler(event:FlexEvent):void
        {
            circle.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown) 
            circle.addEventListener(MouseEvent.MOUSE_UP, mouseReleased);
        }

    ]]>
</fx:Script>

<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<s:Rect id="target1" x="0" y="10" width="150" height="150">
    <s:fill>
        <s:SolidColor color="0xFF0000"/>
    </s:fill>
</s:Rect>

<s:Rect id="target2" x="200" y="10" width="150" height="150">
    <s:fill>
        <s:SolidColor color="0x00FF00"/>
    </s:fill>
</s:Rect>

<s:Graphic id="circle" x="200" y="200">
    <s:Ellipse height="100" width="250">
        <s:stroke>
            <s:SolidColorStroke color="0x000000" weight="2"/>
        </s:stroke>
        <s:fill>
            <s:RadialGradient>
                <s:entries>
                    <s:GradientEntry color="0x0056FF" ratio="0.00" alpha="0.5"/>
                    <s:GradientEntry color="0x00CC99" ratio="0.33" alpha="0.5"/>
                    <s:GradientEntry color="0xECEC21" ratio="0.66" alpha="0.5"/>
                </s:entries>
            </s:RadialGradient>
        </s:fill>
    </s:Ellipse>
</s:Graphic>

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

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

发布评论

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

评论(3

喜爱皱眉﹌ 2024-08-31 14:17:40

试试这个代码:

    import mx.core.mx_internal;
    use namespace mx_internal;

    private function mouseDown(event:MouseEvent):void {
        circle.startDrag(false, new Rectangle(0,0 , 400, 400));
    }


    private function mouseReleased(event:MouseEvent):void {
        circle.stopDrag();

        circle.x = circle.mx_internal::$x;
        circle.y = circle.mx_internal::$y;

        trace("ended drag X: " + circle.x + ", Y: " + circle.y);
    }


    protected function application1_creationCompleteHandler(event:FlexEvent):void
    {
        circle.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown) 
        circle.addEventListener(MouseEvent.MOUSE_UP, mouseReleased);
    }

]]>

try this code:

    import mx.core.mx_internal;
    use namespace mx_internal;

    private function mouseDown(event:MouseEvent):void {
        circle.startDrag(false, new Rectangle(0,0 , 400, 400));
    }


    private function mouseReleased(event:MouseEvent):void {
        circle.stopDrag();

        circle.x = circle.mx_internal::$x;
        circle.y = circle.mx_internal::$y;

        trace("ended drag X: " + circle.x + ", Y: " + circle.y);
    }


    protected function application1_creationCompleteHandler(event:FlexEvent):void
    {
        circle.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown) 
        circle.addEventListener(MouseEvent.MOUSE_UP, mouseReleased);
    }

]]>

南七夏 2024-08-31 14:17:40

主要的巨魔,但是:circle.getBounds() 是一个更好的解决方案,不需要额外的工作。

Major troll, but: circle.getBounds() is a better solution that doesn't require extra work.

情深缘浅 2024-08-31 14:17:40

我认为问题在于数据仅在调用函数时传递。更简单地说,您要检查的信息没有更新。所以你可能想尝试以下操作:

circle.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
circle.addEventListener(MouseEvent.MOUSE_UP, mouseReleased);

private function mouseDown(event:MouseEvent):void {
    addEventListener(Event.ENTER_FRAME, dragMe);
}
function dragMe (e:Event) {
    circle.startDrag(false, new Rectangle(0,0 , 400, 400));
    trace("starting drag X: " + circle.x + ", Y: " + circle.y);
}
private function mouseReleased(event:MouseEvent):void {
    circle.stopDrag();
    trace("ended drag X: " + circle.x + ", Y: " + circle.y);
}

像这样的事情。使用 ENTER_FRAME 事件使 Flash 每帧更新并传递数据。因此默认情况下,该频率为每秒 24 次。 (但这意味着 Flash 每秒也会跟踪 24 次)。我认为没有必要对 mouseReleased 函数执行此操作,因为该位置只需要检查一次。

I think the problem is that the data is passed ONLY when the function is called. More simply said, the info you want to check doesn't get updated. So you might wanna try the following:

circle.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
circle.addEventListener(MouseEvent.MOUSE_UP, mouseReleased);

private function mouseDown(event:MouseEvent):void {
    addEventListener(Event.ENTER_FRAME, dragMe);
}
function dragMe (e:Event) {
    circle.startDrag(false, new Rectangle(0,0 , 400, 400));
    trace("starting drag X: " + circle.x + ", Y: " + circle.y);
}
private function mouseReleased(event:MouseEvent):void {
    circle.stopDrag();
    trace("ended drag X: " + circle.x + ", Y: " + circle.y);
}

Something like this. Using the ENTER_FRAME event makes Flash update and pass data every frame. So by default, that would be 24 times per second. (This will mean though, that Flash traces 24 times a second as well). I think it won't be necessary to do this for the mouseReleased function, because that position needs to be checked only once.

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