开发者

why startDrag() and stopDrag() don't effect the x,y coordinates?

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;

开发者_Python百科        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>


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

]]>


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜