2

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>

4

3 回答 3

1

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

于 2010-11-01T05:14:59.220 回答
1

试试这个代码:

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

]]>

于 2010-06-03T11:56:02.200 回答
0

我认为问题在于仅在调用函数时才传递数据。更简单地说,您要检查的信息不会更新。因此,您可能想尝试以下方法:

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 函数执行此操作,因为该位置只需要检查一次。

于 2011-06-09T10:31:31.360 回答