显示对象的 ActionScript 本地 X 和 Y 坐标?
我试图从精灵中追踪 x 和 y 坐标。我在舞台上添加了一个矩形:
var rect:Rectangle = new Rectangle(10, 10, 200, 200);
addChild(rect);
rect.x = rect.y = 100;
向矩形添加一个 Mouse_Move 事件,我可以跟踪 mouseX 和 mouseY 以在矩形上移动时接收舞台的坐标,但如何获取本地 x 和 y 坐标?因此,如果我将鼠标悬停在矩形精灵的左上角,则 mouseX 和 mouseY 返回 10 作为全局坐标,但如何使其返回 0 和精灵的局部坐标?
我假设 localX 和 localY 是我正在寻找的,但这不起作用:
function mouseOverTraceCoords(evt:MouseEvent):void
{
trace(mouseX, mouseY, evt.localX, evt.localY);
}
i'm trying to trace the x and y coordinates from within a sprite. i've added a rectangle to the stage:
var rect:Rectangle = new Rectangle(10, 10, 200, 200);
addChild(rect);
rect.x = rect.y = 100;
adding a Mouse_Move event to the rect, i can trace mouseX and mouseY to receive the coordinates of the stage while moving over the rect, but how do i get the local x and y coordinates? so if i mouse over the very top left of the rect sprite, the mouseX and mouseY return 10 as the global coordinates, but how do i make it return 0 and the local coordinates of the sprite?
i assumed localX and localY was what i was looking for, but this doesn't work:
function mouseOverTraceCoords(evt:MouseEvent):void
{
trace(mouseX, mouseY, evt.localX, evt.localY);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这取决于您如何设计 Rectangle 类,
如果你做了这样的事情:
你的本地坐标将在
0,0
但你的绘图从x,y
开始,所以当你获取localx 时, localY
您将获得x,y
而不是0,0
。但是,如果您的类被设计为类似的东西:
那么您的绘图从
0,0
开始,并且您的对象在x,y
移动,因此localX
来自MouseEvent
的 code> 和localY
就可以了。编辑:
要获取局部坐标,您可以尝试使用 getBounds :
It depends how you have designed your Rectangle class,
if you have done something like this:
your local coordinate will be at
0,0
but you drawing start atx,y
so when you are getting thelocalx, localY
you will obtainx,y
and not0,0
.But if your class is designed as something like that :
then your drawing start at
0,0
and your object is moved atx,y
so thelocalX
andlocalY
from theMouseEvent
will be ok.Edit:
To get the local coordinate you can try using getBounds :
看看 globalToLocal< /a>
take a look at globalToLocal
根本不需要做任何转换*,您的矩形将有两个名为
mouseX
和mouseY
的属性,其中包含鼠标的本地坐标。如果您在矩形内绘制图形偏移,则坐标系不会考虑这一点,最好偏移整个矩形。
* 假设您的 Rectangle 类是 DisplayObject 的子类,我会假设您已经将它放在舞台上了。
There's no need to do any transformations at all *, your Rectangle will have two properties called
mouseX
andmouseY
which contain the local coordinates of the mouse.If you're drawing your graphics offset within the rectangle, the coordinate system will not consider this, it's a better idea to offset the entire rectangle instead.
* assuming your Rectangle class is a subclass of DisplayObject, which I'd assume since you've got it on the stage.