When executing the code:
var cont:Sprite = new Sprite();
var a:Vector.<int > = Vector.<int > ([1,2]);
var b:Vector.<Number > = Vector.<Number > ([0,0,40,40]);
cont.graphics.lineStyle(5, 0x442299);
cont.graphics.drawPath(a, b);
addChild( cont );
cont.x = 100;
cont.y = 100;
trace("X coordinate of purple line: ", cont.x);
I get the output “X coordinate of purple line: 100”
However, when I test this code and draw a line from (100, 100) to (140, 140) with the mouse:
var line:Sprite = new Sprite();
stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
var startX:int = -1;
var startY:int = -1;
function mouseDownHandler(event:MouseEvent):void
{
startX = mouseX;
startY = mouseY;
}
function mouseUpHandler(event:MouseEvent):void
{
swype(Vector.<int> ([1,2]), Vector.<Number> ([startX,startY,mouseX,mouseY]));
}
function swype(commands:Vector.<int>, coords:Vector.<Number>):void
{
var container:Sprite = new Sprite();
container.graphics.lineStyle(5, 0x0066CC);
container.graphics.drawPath(commands, coords);
addChild( container );
container.x = 100;
container.y = 100;
trace("X coordinate of blue line: ", container.x);
}
I get the output: “X coordinate of blue line: 0”
Why is it that when I am getting the coordinates from the mouse’s position on the screen and adding them to the vector, the Sprite container’s x and y coordinates default to 0,0?
Not sure what you are wanting to do but you may need to use the localToGlobal methods if you keep moving that _container around 😉
This has to do with the context of where you get the property of the mouseX and mouseY.
You have to think more about the display stack and how that relates to what the properties of mouseX and Y will give you.
Have a try of this and shoot me any questions if you cannot understand why it works.