Skip to main content
Inspiring
September 15, 2011
Answered

Global to Local Mouse Cordinates

  • September 15, 2011
  • 1 reply
  • 2570 views

in my Controller Class:

stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener);

//for snapping my object to a grid

//im using this algorithm

protected function mouseMoveListener(e:MouseEvent):void {
            brick.x = mouseX - mouseX%theGrid.getGridSize();
            brick.y = mouseY - mouseY%theGrid.getGridSize();
}


brick it's inside a container and its position in x axis is 200px

When the brick is moving, the x and y value are based on stage x and y mouse.

The 0 in the stage should correspond to -200 inside my container.

How can i use globalToLocal method?

instead write:

brick.x = (mouseX - mouseX%theGrid.getGridSize())-200;

?

thanks

This topic has been closed for replies.
Correct answer kglad

:


stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener);

//for snapping my object to a grid

//im using this algorithm

protected function mouseMoveListener(e:MouseEvent):void {
           // brick.x = mouseX - mouseX%theGrid.getGridSize();
           // brick.y = mouseY - mouseY%theGrid.getGridSize();

var globalPt:Point=new Point(mouseX,mouseY);

var localPt:Point = brick.globalToLocal(globalPt);

trace(localPt.x,localPt.y);

}


1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
September 15, 2011

:


stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener);

//for snapping my object to a grid

//im using this algorithm

protected function mouseMoveListener(e:MouseEvent):void {
           // brick.x = mouseX - mouseX%theGrid.getGridSize();
           // brick.y = mouseY - mouseY%theGrid.getGridSize();

var globalPt:Point=new Point(mouseX,mouseY);

var localPt:Point = brick.globalToLocal(globalPt);

trace(localPt.x,localPt.y);

}


Inspiring
September 15, 2011

Thanks a lot!!!

kglad
Community Expert
Community Expert
September 15, 2011

you're welcome.