views:

279

answers:

3

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);
}
+1  A: 

take a look at globalToLocal

maxmc
+1  A: 

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.

grapefrukt
when i call rect.mouseX and rect.mouseY, i get the same coordinates as the stage's mouseX and mouseY
TheDarkInI1978
i mean, if i add the rect in the middle of the stage, so it's not located at X=0, Y=0, which would return the same coordinates, they are still the same.
TheDarkInI1978
i updated my answer a bit.
grapefrukt
+1  A: 

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);
}
Patrick
it's designed as in your first example. so how can i receive local coordinates from that? the rect sprite is draggable, so that's why i can't assign it's position as in your second example.
TheDarkInI1978
@TheDarkIn1978 Ok i have edited my answer see if it works in your app.
Patrick
thanks. it turned out to be a lot easier than i was anticipating. i simply had to subtract evt.localX from getRect(this).x to receive the local coordinates. it took me far too long to solve this.
TheDarkInI1978