当我使用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>
答案 0 :(得分: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);
}
]]>
答案 1 :(得分:1)
主要的巨魔,但是:circle.getBounds()是一个更好的解决方案,不需要额外的工作。
答案 2 :(得分: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函数执行此操作,因为该位置只需要检查一次。