In most games that require user clicks, the conversion of coordinates is a method that must be mastered.
First in a 700x700 stage to create 2 blocks, the red large square A is 600x600, located in (50,50), green small square B is 300x300. A is the parent container of B, and B is located relative to the stage (200,200).
Next, do a series of tests to illustrate how these 2 methods work.
Let's say we point to the center point of the B block, which is global (350,350), A (300,300), B (150,150), and see what the results will look like:
Private Function OnClickHandler (e:mouseevent): void
{
E.stoppropagation ();
var stagepoint:point = new Point (Stage.mousex, Stage.mousey);
Trace (stagepoint.x);//350
Trace (STAGEPOINT.Y);//350
Trace (Stage.globaltolocal (stagepoint). x);//350
Trace (Stage.globaltolocal (stagepoint). y);//350
Trace (Smallshape.globaltolocal (stagepoint). x);//150
Trace (Smallshape.globaltolocal (stagepoint). y);//150
Trace (Bigshape.globaltolocal (stagepoint). x);//300
Trace (Bigshape.globaltolocal (stagepoint). y);//300
var localpoint:point = new Point (E.localx, e.localy);
Trace (localpoint.x);//150
Trace (LOCALPOINT.Y);//150
Trace (Stage.localtoglobal (localpoint). x);//150
Trace (Stage.localtoglobal (localpoint). y);//150
Trace (Smallshape.localtoglobal (localpoint). x);//350
Trace (Smallshape.localtoglobal (localpoint). y);//350
Trace (Bigshape.localtoglobal (localpoint). x);//200
Trace (Bigshape.localtoglobal (localpoint). y);//200
}
The test results show that the Globaltolocal method invokes the same global coordinate (350,350) by whom it is converted to whose local coordinates. The Localtoglobal method is slightly more complicated, and whoever calls Localtoglobal is the equivalent of adding the caller's coordinates to the stage on the basis of Localpoint (150,150).
After understanding how these 2 methods work, it's easy to say that we're going to turn it into a block by clicking on the B-block position:
Private Function OnClickHandler (e:mouseevent): void
{
var localpoint:point = new Point (E.localx, e.localy);
var stagepoint:point = Smallshape.localtoglobal (Localpoint),//because the clicked position is within the B block, so called by B
Localpoint = bigshape.globaltolocal (stagepoint);//Click on the coordinates of the A block
}
AS3 Global and local coordinate transformations