Migrating _xmouse to as3

  • deafdigit
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Oct 06, 2009
  • Posts: 7
  • Status: Offline

Post October 18th, 2009, 4:24 am

Hey guys,


I've scoured what feels like the entire internet to get an answer for this question, but it just doesn't seem to be out there, so I figured I'd get a straight answer once and for all.

In as2, attaching _xmouse to a object would return the mouse_coords relative to that object.
I know that _xmouse have changed to mouseX in as3, but this seems to return the absolute mouse-coords no matter what I attach it to, regardless that it is actually attachable to objects:

Code: [ Select ]
import flash.display.Sprite;
import flash.events.MouseEvent;

var square:Sprite = new Sprite();
square.graphics.beginFill(0xFF0000);
square.graphics.drawRect(100, 100, 100, 100);
addChild(square);

square.addEventListener(MouseEvent.CLICK, traceCoordinates);
stage.addEventListener(MouseEvent.CLICK, traceCoordinates2);

function traceCoordinates(e:Event):void {
  trace(square.mouseX, square.mouseY);
}

function traceCoordinates2(e:Event):void {
  trace(mouseX, mouseY);
}
  1. import flash.display.Sprite;
  2. import flash.events.MouseEvent;
  3. var square:Sprite = new Sprite();
  4. square.graphics.beginFill(0xFF0000);
  5. square.graphics.drawRect(100, 100, 100, 100);
  6. addChild(square);
  7. square.addEventListener(MouseEvent.CLICK, traceCoordinates);
  8. stage.addEventListener(MouseEvent.CLICK, traceCoordinates2);
  9. function traceCoordinates(e:Event):void {
  10.   trace(square.mouseX, square.mouseY);
  11. }
  12. function traceCoordinates2(e:Event):void {
  13.   trace(mouseX, mouseY);
  14. }


The above code returns the absolute value of the mouse-coords no matter where I click - on the square, or on the stage.

So my question is this: Is there a way to get the mouse-coords relative to an object in AS3?


As always, thank you all in advance,
// deafdigit
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post October 18th, 2009, 4:24 am

  • IceCold
  • Guru
  • Guru
  • User avatar
  • Joined: Nov 05, 2004
  • Posts: 1254
  • Loc: Ro
  • Status: Offline

Post October 19th, 2009, 2:40 am

Strange situation, but i'm afraid flash it's quite correct about what it returns, and in it's point of view, the coordinates are local :D
Let me explain what you do.
1. you create a new sprite, and since it's an empty sprite, it will start from 0,0 with a 0 x 0 width x height.
2. inside that sprite, starting from 100, 100, you draw a square. Flash consider that your sprite is starting from 0, 0 and you only draw the rectangle inside it. It calculates the width and height based on the width/height of the objects inside the sprite, but the coordinates are calculated from 0, 0, even if it doesn't consider the empty space as being part of the sprite.
So instead of creating the rectangle at 100, 100, create it at 0,0
and then move the square to 100, 100.
Code: [ Select ]
square.graphics.drawRect(0, 0, 100, 100);
square.x = 100; square.y = 100;
  1. square.graphics.drawRect(0, 0, 100, 100);
  2. square.x = 100; square.y = 100;
“True mastery transcede any particular art. It stems from mastery of oneself - the ability, developed throgh self-discipline, to be calm, fully aware, and complety in tune with oneself and the surroundings. Then, and only then, can a person know himself. ”

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 54 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.