Skip to main content
Known Participant
July 5, 2009
Answered

my xpos and yposition on stage does not work properly

  • July 5, 2009
  • 1 reply
  • 808 views


stage.addEventListener(MouseEvent.MOUSE_MOVE,action);

function action(event:MouseEvent)
{
    //trace("You have double clicked"+event.localX);
   
    box.x=event.localX;
    box.y=event.localY;
   
   
}

This topic has been closed for replies.
Correct answer Ned Murphy

What are you trying to accomplish?  You might be misinterpretting what the event.localX/Y properties represent.  event.localX and event.localY do not necessarily provide the x and y position of mouse in the stage (which you probably thought from the answer you accepted in another posting you have)

Try this code (with your box object still around) and you should get the correct idea of what event.localX/Y is providing... Click on the empty stage area as well as the box object.

stage.addEventListener(MouseEvent.CLICK,action);

function action(event:MouseEvent)
{
trace(event.localX);
trace(event.localY);
}

From that you should see that event.localX/Y are relative to the object where the event occurs, not the stage.

So for your code you are probably seeing the box acting erratically, moving from following the cursor to jumping to some distant corner of the stage (depends on the box size and where it axis lies).  That's because the localX is switching between the stage and the box coordinate system as you continuously move the box to and from the cursor location.

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
July 5, 2009

What are you trying to accomplish?  You might be misinterpretting what the event.localX/Y properties represent.  event.localX and event.localY do not necessarily provide the x and y position of mouse in the stage (which you probably thought from the answer you accepted in another posting you have)

Try this code (with your box object still around) and you should get the correct idea of what event.localX/Y is providing... Click on the empty stage area as well as the box object.

stage.addEventListener(MouseEvent.CLICK,action);

function action(event:MouseEvent)
{
trace(event.localX);
trace(event.localY);
}

From that you should see that event.localX/Y are relative to the object where the event occurs, not the stage.

So for your code you are probably seeing the box acting erratically, moving from following the cursor to jumping to some distant corner of the stage (depends on the box size and where it axis lies).  That's because the localX is switching between the stage and the box coordinate system as you continuously move the box to and from the cursor location.

nepaliktoAuthor
Known Participant
July 7, 2009

Oh ! Ya i got , localX localY identify position of any display Object or display Object Container. Now how to find out mouse x and y position . Thanks for reply

Ned Murphy
Legend
July 7, 2009

See if using mouseX and mouseY do what you want.