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?
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?