显示对象的 ActionScript 本地 X 和 Y 坐标?

发布于 2024-09-03 00:33:39 字数 487 浏览 2 评论 0原文

我试图从精灵中追踪 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

-黛色若梦 2024-09-10 00:33:39

这取决于您如何设计 Rectangle 类,
如果你做了这样的事情:

class Rect1 extends Sprite {
    public function Rect1(x:Number, y:Number, width:Number, height:Number) {
        var g:Graphics = graphics;
        g.beginFill(0xff0000, 0.5);
        g.drawRect(x, y, width, height);
        g.endFill();
    }
}

你的本地坐标将在 0,0 但你的绘图从 x,y 开始,所以当你获取 localx 时, localY 您将获得 x,y 而不是 0,0

但是,如果您的类被设计为类似的东西:

class Rect2 extends Sprite {
    public function Rect2(x:Number, y:Number, width:Number, height:Number) {
        var g:Graphics = graphics;
        g.beginFill(0xff0000, 0.5);
        g.drawRect(0, 0, width, height);
        g.endFill();

        this.x = x;
        this.y = y;
    }
}

那么您的绘图从 0,0 开始,并且您的对象在 x,y 移动,因此 localX来自 MouseEvent 的 code> 和 localY 就可以了。

编辑:
要获取局部坐标,您可以尝试使用 getBounds :

function mouseOverTraceCoords(evt:MouseEvent):void {
 var dob:DisplayObject = DisplayObject(evt.target);
 var bnds:flash.geom.Rectangle = getBounds(dob.parent);
 var localX:Number=e.localX - bnds.x + dob.x;
 var localY:Number=e.localY - bnds.y + dob.y;

 trace(mouseX, mouseY, evt.localX, evt.localY, localX, localY);
}

It depends how you have designed your Rectangle class,
if you have done something like this:

class Rect1 extends Sprite {
    public function Rect1(x:Number, y:Number, width:Number, height:Number) {
        var g:Graphics = graphics;
        g.beginFill(0xff0000, 0.5);
        g.drawRect(x, y, width, height);
        g.endFill();
    }
}

your local coordinate will be at 0,0 but you drawing start at x,y so when you are getting the localx, localY you will obtain x,y and not 0,0.

But if your class is designed as something like that :

class Rect2 extends Sprite {
    public function Rect2(x:Number, y:Number, width:Number, height:Number) {
        var g:Graphics = graphics;
        g.beginFill(0xff0000, 0.5);
        g.drawRect(0, 0, width, height);
        g.endFill();

        this.x = x;
        this.y = y;
    }
}

then your drawing start at 0,0 and your object is moved at x,y so the localX and localY from the MouseEvent will be ok.

Edit:
To get the local coordinate you can try using getBounds :

function mouseOverTraceCoords(evt:MouseEvent):void {
 var dob:DisplayObject = DisplayObject(evt.target);
 var bnds:flash.geom.Rectangle = getBounds(dob.parent);
 var localX:Number=e.localX - bnds.x + dob.x;
 var localY:Number=e.localY - bnds.y + dob.y;

 trace(mouseX, mouseY, evt.localX, evt.localY, localX, localY);
}
以酷 2024-09-10 00:33:39

根本不需要做任何转换*,您的矩形将有两个名为 mouseXmouseY 的属性,其中包含鼠标的本地坐标。

如果您在矩形内绘制图形偏移,则坐标系不会考虑这一点,最好偏移整个矩形。

* 假设您的 Rectangle 类是 DisplayObject 的子类,我会假设您已经将它放在舞台上了。

There's no need to do any transformations at all *, your Rectangle will have two properties called mouseX and mouseY 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文