为什么 startDrag() 和 stopDrag() 不影响 x,y 坐标?
当我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个代码:
try this code:
主要的巨魔,但是:circle.getBounds() 是一个更好的解决方案,不需要额外的工作。
Major troll, but: circle.getBounds() is a better solution that doesn't require extra work.
我认为问题在于数据仅在调用函数时传递。更简单地说,您要检查的信息没有更新。所以你可能想尝试以下操作:
像这样的事情。使用 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:
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.