PDA

View Full Version : Confused about local and stage coordinates


web developer
11-16-2008, 04:15 PM
Hello,

I'm trying to make sense out of the coordinate system. Here's my code:

package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;

public class Main extends Sprite
{
private var circle:Sprite;
private const RADIUS:int = 40;

public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}

private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
circle = new Sprite();

stage.addChild(circle);
circle.graphics.beginFill(0x439391);
circle.graphics.drawCircle(300, 300, RADIUS);
circle.graphics.endFill();

circle.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
circle.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}

private function onMouseDown(event:MouseEvent):void
{
trace("\nMouse down");
trace("Local x: " + event.localX);
trace("Local y: " + event.localY);
trace("Stage x: " + event.stageX);
trace("Stage y: " + event.stageY);
circle.startDrag();
circle.addEventListener(MouseEvent.MOUSE_MOVE, move);
}

private function onMouseUp(event:MouseEvent):void
{
trace("\nMouse up");
circle.stopDrag();
circle.removeEventListener(MouseEvent.MOUSE_MOVE, move);
}

private function move(event:MouseEvent):void
{
trace("\nCircle x: " + circle.x);
trace("Circle y: " + circle.y);
}
}

}


event.localX and event.localY always print a values within the area of the orginal circle, as if the circle is always centered at (300, 300). In addition, circle.x and circle.y print values relative to the circles original position, even though the documentation (http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/DisplayObject.html#x) says the x value is relative to the parent object, which is the stage. Therefore, I get negative values if the move the circle towards the left.
Is there a method/setting for getting the sprites coordinates within the stage?

rawmantick
11-17-2008, 04:24 AM
In addition, circle.x and circle.y print values relative to the circles original position, even though the documentation says the x value is relative to the parent object, which is the stage

circle.x and circle.y are coordinates of the circle on the stage.

event.localX and event.localY are coordinates of mouse pointer on the circle no matter where it is (on the stage, or not).

So, event.localX and event.localY can be negative easily. Imagine you have a symbol where the circle is drawn with the center at symbols (0,0) and radius 10. So for example point (-3,-3) does belong to you circle. And if you click the point, you will get event.localX=-3 and event.localY=-3.

So I don't see any contradiction. Imho flash player display tree system and its relativeness is a very great thing. And docs absolutely correspond with it.

web developer
11-18-2008, 01:08 PM
Thank you for your response, but I am still confused


event.localX and event.localY are coordinates of mouse pointer on the circle no matter where it is (on the stage, or not).

Therefore, these cooridinates assume the original center of the circle, no matter where the circle is moved, correct?


circle.x and circle.y are coordinates of the circle on the stage.


I drew the circle at position (300, 300). However, when I print circle.x and circle.y, why do I get (0, 0) as the top left coordinate instead of (260, 260) if the top left corner of the stage is at (0, 0)? When I move the circle toward the top left corner, the circle.x and circle.y are negative.

rawmantick
11-18-2008, 01:59 PM
Therefore, these cooridinates assume the original center of the circle, no matter where the circle is moved, correct?

Circle, square, triangle, hexaone... no matter.

You have a sprite. It has one abstract point. The point is its center. When you set sprite's coordinates (say coordinates of the sprite on its parent) - you move that abstract point. That's it.

But the thing, is that oll graphics on your sprite is a set of points with their coordinates. And coordinates are relative to sprite's center.

Just try to waste every question you have about relativeness from your head and imagine... You have a list of paper. Let it be square. Right in the center of that paper you have a little black point. It is the center of you symbol. All symbol's coordinates are measured from it. So when the center's coordinates are measure from itself, ofcourse they are (0,0).

Now imagine you click on thjis paper. You can click lefter or righter, higher or lower than center - and you get x and y less or more than 0.

Now imagine you drag the paper on the surface of another. What will change ?